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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.