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

99封情书 提交于 2019-12-13 03:02:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!