问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/50071727/pandas-use-to-csv-with-quotation-marks-and-a-comma-as-a-seperator