Python CSV:从值中删除引号

时间:2022-09-15 14:50:46

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

我有一个过程,可以下载,编辑然后再次上传CSV文件。在下载时,CSV文件的格式正确,没有包装双引号

1, someval, someval2

When I open the CSV in a spreadsheet, edit and save, it adds double quotes around the strings

当我在电子表格中打开CSV,编辑并保存时,它会在字符串周围添加双引号

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.

我认为这只是电子表格的动作(在这种情况下,openoffice)。我希望我的上传脚本删除包装双引号。我无法删除所有引号,只是包含正文包含它们,我也不想只检查双引号的第一个和最后一个字符。

Im almost sure that the CSV library in python would know how to handle this, but not sure how to use it...

我几乎可以肯定python中的CSV库会知道如何处理这个,但不知道如何使用它...

EDIT When I use the values within a dictionary, they turn out as follows

编辑当我使用字典中的值时,它们如下所示

{'header':'"value"'}

Thanks

谢谢

3 个解决方案

#1


14  

For you example, the following works:

对于您的示例,以下工作:

import csv
writer = csv.writer(open("out.csv", "wb"), quoting=csv.QUOTE_NONE)
reader = csv.reader(open("in.csv", "rb"), skipinitialspace=True)
writer.writerows(reader)

You might need to play with the dialect options of the CSV reader and writer -- see the documentation of the csv module.

您可能需要使用CSV阅读器和编写器的方言选项 - 请参阅csv模块的文档。

#2


6  

Thanks to everyone who was trying to help me, but I figured it out. When specifying the reader, you can define the quotechar

感谢所有试图帮助我的人,但我明白了。指定阅读器时,您可以定义quotechar

csv.reader(upload_file, delimiter=',', quotechar='"')

This handles the wrapping quotes of strings.

这会处理字符串的包装引号。

#3


3  

For Python 3:

对于Python 3:

import csv
writer = csv.writer(open("query_result.csv", "wt"), quoting=csv.QUOTE_NONE, escapechar='\\')
reader = csv.reader(open("out.txt", "rt"), skipinitialspace=True)
writer.writerows(reader)

The original answer gives this error under Python 3. Also See this SO for detail: csv.Error: iterator should return strings, not bytes

原始答案在Python 3下给出了这个错误。另请参阅此SO以获取详细信息:csv.Error:迭代器应返回字符串,而不是字节

Traceback (most recent call last): File "remove_quotes.py", line 11, in writer.writerows(reader) _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

回溯(最近一次调用最后一次):文件“remove_quotes.py”,第11行,在writer.writerows(reader)中_csv.Error:迭代器应该返回字符串,而不是字节(你是否在文本模式下打开文件?)

#1


14  

For you example, the following works:

对于您的示例,以下工作:

import csv
writer = csv.writer(open("out.csv", "wb"), quoting=csv.QUOTE_NONE)
reader = csv.reader(open("in.csv", "rb"), skipinitialspace=True)
writer.writerows(reader)

You might need to play with the dialect options of the CSV reader and writer -- see the documentation of the csv module.

您可能需要使用CSV阅读器和编写器的方言选项 - 请参阅csv模块的文档。

#2


6  

Thanks to everyone who was trying to help me, but I figured it out. When specifying the reader, you can define the quotechar

感谢所有试图帮助我的人,但我明白了。指定阅读器时,您可以定义quotechar

csv.reader(upload_file, delimiter=',', quotechar='"')

This handles the wrapping quotes of strings.

这会处理字符串的包装引号。

#3


3  

For Python 3:

对于Python 3:

import csv
writer = csv.writer(open("query_result.csv", "wt"), quoting=csv.QUOTE_NONE, escapechar='\\')
reader = csv.reader(open("out.txt", "rt"), skipinitialspace=True)
writer.writerows(reader)

The original answer gives this error under Python 3. Also See this SO for detail: csv.Error: iterator should return strings, not bytes

原始答案在Python 3下给出了这个错误。另请参阅此SO以获取详细信息:csv.Error:迭代器应返回字符串,而不是字节

Traceback (most recent call last): File "remove_quotes.py", line 11, in writer.writerows(reader) _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

回溯(最近一次调用最后一次):文件“remove_quotes.py”,第11行,在writer.writerows(reader)中_csv.Error:迭代器应该返回字符串,而不是字节(你是否在文本模式下打开文件?)