我有一个可以下载、编辑然后再次上传 CSV 文件的流程。下载时,CSV 文件格式正确,没有双引号
1, someval, someval2
当我在电子表格中打开 CSV、编辑并保存时,它会在字符串周围添加双引号,
1, "someEditVal", "someval2"
我认为这只是电子表格的操作(在本例为 openoffice)。我希望我的上传脚本删除双引号。我无法删除所有引号,以防正文包含它们,而且我也不想只检查第一个和最后一个字符是否有双引号。
我几乎确定 python 中的 CSV 库知道如何处理这个问题,但不确定如何使用它......
编辑
当我使用字典中的值时,结果如下
{'header':'"value"'}
谢谢
I have a process where a CSV file can be downloaded, edited then uploaded again. On the download, the CSV file is in the correct format, with no wrapping double quotes
1, someval, someval2
When I open the CSV in a spreadsheet, edit and save, it adds double quotes around the strings
1, "someEditVal", "someval2"
I figured this was just the action of the spreadsheet (in this case, openoffice). I want my upload script to remove the wrapping double quotes. I cannot remove all quotes, just incase the body contains them, and I also dont want to just check first and last characters for double quotes.
Im almost sure that the CSV library in python would know how to handle this, but not sure how to use it...
EDIT
When I use the values within a dictionary, they turn out as follows
{'header':'"value"'}
Thanks
发布评论
评论(3)
对于您的示例,以下工作有效:
您可能需要使用 CSV 读取器和写入器的方言选项 - 请参阅
csv
模块的文档。For you example, the following works:
You might need to play with the dialect options of the CSV reader and writer -- see the documentation of the
csv
module.感谢所有试图帮助我的人,但我想通了。指定阅读器时,您可以定义 quotechar
它处理字符串的换行引号。
Thanks to everyone who was trying to help me, but I figured it out. When specifying the reader, you can define the quotechar
This handles the wrapping quotes of strings.
对于Python 3:
原始答案在Python 3下给出了此错误。另请参阅此SO以了解详细信息:csv.Error:迭代器应该返回字符串,而不是字节
For Python 3:
The original answer gives this error under Python 3. Also See this SO for detail: csv.Error: iterator should return strings, not bytes