Python data frame Export to csv with Quotation marks (")

送分小仙女□ 提交于 2020-01-14 19:05:14

问题


How to export python dataframe to csv with double quotes. I have tried with below code but its not coming in output file. I need results like "column1","column2",column3"... Please help.

exportPath=exportPath+'\\data2Upload.csv'
header=['Country','Indicator','Unit','Frequency','Date','Value']
data.to_csv(exportPath,columns=header,sep=',',quotechar='"',index=False)

回答1:


You can tell pandas to quote everything by default using the csv.QUOTE_ALL property:

data.to_csv(exportPath, columns=header,sep=",",quotechar='"',index=False,
            quoting=csv.QUOTE_ALL)

Official Python docs: https://docs.python.org/2/library/csv.html#csv.QUOTE_ALL



来源:https://stackoverflow.com/questions/43719802/python-data-frame-export-to-csv-with-quotation-marks

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