简体   繁体   English

使用Python的csv.reader处理转义的引号

[英]Handling escaped quotes with Python's csv.reader

Using python's csv module, I'm trying to read some CSV data. 使用python的csv模块,我正在尝试读取一些CSV数据。

I'm using the code: 我正在使用代码:

dialect = csv.Sniffer().sniff(csv_file.read(1024))
csv_file.seek(0)
reader = csv.reader(csv_file, dialect)

for line in reader:
    ...

Everything works fine except for lines containing escaped quotes: 除了包含转义引号的行外,一切正常:

11837,2,NULL,"\"The Take Over, The Breaks Over\"","Fall Out Boy"

Such a line is tokenized as: 这样的行被标记为:

['11837', '2', 'NULL', '\\The Take Over', ' The Breaks Over\\""', 'Fall Out Boy']

The dialect contains the following properties: 方言包含以下属性:

dialect.quotechar = "
dialect.quoting = 0
dialect.escapechar = None
dialect.delimiter = ,
dialect.doublequote = False
dialect.lineterminator = \n

Is there anything I can do besides writing my own CSV parser? 除了编写自己的CSV解析器之外,还有什么可以做的吗?

If I'm not mistaken, dialect.escapechar = None should be dialect.escapechar = '\\\\' 如果我没弄错的话, dialect.escapechar = None应该是dialect.escapechar = '\\\\'

If you look at the examples in the docs , it certainly seems to suggest making that alteration 如果你看一下文档中的例子 ,肯定似乎建议做出改变

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM