PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 30, 2022

[FIXED] How to write list which contains comma in csv file using python?

 August 30, 2022     csv, list, python     No comments   

Issue

List contains below data,

['Obama', 'John Barta', 'IN, 33, 33', '444', '']

I am trying to write this data into a csv file using below code,

with open(output_file, 'w') as writeFile:
    writer = csv.writer(writeFile, delimiter=',', quotechar="'", quoting=csv.QUOTE_NONE)
    writer.writerow(colValues)

But getting below output in csv file,

"Obama","John Barta",'"IN, 33, 33"',"444",""

I don't want single quote between '"IN, 33, 33"'.

Desired output:

"Obama","John Barta","IN, 33, 33","444",""


Solution

From your sample output, it looks like you want to quote all fields. So the straightforward way to do it would be with:

writer = csv.writer(writeFile, delimiter=',',
               quotechar='"', quoting=csv.QUOTE_ALL)

I also changed quotechar to be a double quote, since that's in your sample output also.



Answered By - Win
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing