Pandas: use to_csv() with quotation marks and a comma as a seperator
Asked Answered
B

1

1

I'm new to pandas, and I'm having trouble exporting to the correct CSV format for another system.

Source file looks like this with fields seperated by a quotation marks and a comma.

"User ID","Description","User Name"
"user1","John","john01"
"user2","Mike","mike33"
"user3","Barry","barry66"

I want to export in the same format but I am unable to .

I tried

csvfiledf.to_csv(outputcsv, index=False,  sep='\",\"') 

but get

TypeError: "delimiter" must be a 1-character string
Baxie answered 28/4, 2018 at 0:28 Comment(0)
N
6

The delimiter in your output file is still ,. What you need to modify is the quoting argument:

import csv
csvfiledf.to_csv(outputcsv, index=False, delimiter=',', quoting=csv.QUOTE_ALL) 

The delimiter argument is optional, since , is implied.

Nephrectomy answered 28/4, 2018 at 1:2 Comment(1)
I got this working thanks, but the quoting parameter was an integer and not a boolean - e.g. quoting=csv.QUOTE_ALLBaxie

© 2022 - 2024 — McMap. All rights reserved.