Python CSV:从值中删除引号

发布于 2024-10-13 11:21:04 字数 443 浏览 0 评论 0 原文

我有一个可以下载、编辑然后再次上传 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你是暖光i 2024-10-20 11:21:04

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

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)

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

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.

香草可樂 2024-10-20 11:21:04

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

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

它处理字符串的换行引号。

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

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

This handles the wrapping quotes of strings.

怀念你的温柔 2024-10-20 11:21:04

对于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)

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

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

For 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

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?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文