Python CSV:从值中删除引号 [英] Python CSV: Remove quotes from value

查看:2718
本文介绍了Python CSV:从值中删除引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过程,可以下载,编辑并再次上传CSV文件。在下载时,CSV文件格式正确,不包括双引号

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

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

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

1, "someEditVal", "someval2"

我想这只是电子表格的操作(在这种情况下,openoffice)。我想要我的上传脚本删除包装双引号。我不能删除所有的引号,只是incase的身体包含他们,我也不想只是检查第一个和最后一个字符双引号。

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几乎肯定CSV在python中的库将知道如何处理这个,但不知道如何使用它...

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

EDIT
当我使用字典中的值,如下所示

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

{'header':'"value"'}

感谢

推荐答案

工程:

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 模块的文档

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

这篇关于Python CSV:从值中删除引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆