python - 写入 CSV,为空字符串获取 "Error: need to escape"

标签 python csv

当有人发现我在这里做错了什么时,我可能会感到非常愚蠢,但我发现自己无法克服看起来应该是一个简单错误的错误。

我正在使用 Python 将一些数据写入 CSV。我想写的其中一件事是整数的列表。在将列表写入文件之前,我将其join 成一个字符串:

with open('publishers.csv', 'wb') as f:
    writer = csv.writer(f, quoting=csv.QUOTE_NONE, delimiter='|', quotechar='')
    for item in big_list_of_objects:
        description = item.description
        number_list = item.number_list
        formatted_numbers = "-".join(number_list)
        writer.writerow([
            description,
            formatted_numbers
            ])

number_list 中可能包含从零到一整串数字的任何位置。如果它是一个空列表,join 只是将 formatted_numbers 设置为空字符串。如果它不是空列表,我会得到一个由连字符连接的整数组成的字符串。

number_list = [1,2,34,12]
formatted_numbers = '1-2-34-12'

number_list = []
formatted_numbers = ''

无论如何,这就是想法。实际上,前五行写入成功,然后我得到:

File "<console>", line 1, in <module>
  File "/path/path/path.py", line 500, in offending_function
    formatted_numbers
Error: need to escape, but no escapechar set

现在在这种特殊情况下,成功写入的前五行有一个空的 number_list。持续崩溃的行有一个空的number_list。在该行的 number_list 之前或之后立即写入值并没有什么奇怪的。当出现此错误时写入 formatted_numbers 并没有什么奇怪的 - 我扔了一个 print 语句进行调试,它只是一个空字符串,就像它之前的五个一样.

谁能帮我弄清楚我这里可能哪里出错了?


编辑:我添加了这些打印语句:

with open('publishers.csv', 'wb') as f:
    writer = csv.writer(f, quoting=csv.QUOTE_NONE, delimiter='|', quotechar='')
    for item in big_list_of_objects:
        description = item.description
        print "Description for %r is %r" % (item,description)
        number_list = item.number_list
        print "Now formatting %r for %r" % (number_list,item)
        formatted_numbers = "-".join(number_list)
        print repr(formatted_numbers)
        writer.writerow([
            description,
            formatted_numbers
            ])

结果:

Description for 'p89' is u''
Now formatting '' for 'p89'
''
Description for 'p88' is u''
Now formatting '' for 'p88'
''
Description for 'p83' is u''
Now formatting '' for 'p83'
''
Description for 'p82' is u'in-tr-t91411'
Now formatting '' for 'p82'
''
Description for 'p81' is u''
Now formatting '' for 'p81'
''
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/path/path/path.py", line 501, in offending_function
    formatted_numbers
Error: need to escape, but no escapechar set

p81 未写入 CSV - 这是发生崩溃的地方。但是,如您所见,print repr(formatted_numbers) 显示它是一个与之前的字符串相同的空白字符串。项目 p81 没有描述(只是一个空字符串),但是项目的描述在它之前。

最佳答案

问题很可能是因为您的 description 中有 |,这也是您的 csv 的分隔符。因此,csv 试图转义它,但由于未设置 csv.escapechar 而无法转义。在我的计算机中显示相同问题的示例 -

>>> description = 'asda|sd'
>>> formatted_numbers = ''
>>> with open('a.csv','w') as f:
...     writer = csv.writer(f, quoting=csv.QUOTE_NONE, delimiter='|', quotechar='')
...     writer.writerow([
...             description,
...             formatted_numbers
...             ])
...
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
_csv.Error: need to escape, but no escapechar set

一个解决方法是提供一个 escapechar 以便它可以被转义。示例 -

writer = csv.writer(f, quoting=csv.QUOTE_NONE, delimiter='|', quotechar='',escapechar='\\')    #Or any other appropriate escapechar

或者另一个修复方法是在尝试编写描述之前删除描述中的 |,如果您在描述字段中真的不需要它 -

description = description.replace('|','')

或者您可以引用所有字段,方法是使用 csv.QUOTE_ALL 而不是 csv.QUOTE_NONE 来提供有效的 quotechar

关于python - 写入 CSV,为空字符串获取 "Error: need to escape",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32107790/

相关文章:

python - 如何使用 Python 和 Pyomo 与 Ipopt 获取 Hessian 和拉格朗日梯度来计算 KKT 矩阵

python - 如何在maya中查询当前视口(viewport)渲染器

python - 使用带有日期/时间的 CSV 文件的 loadtext

excel - *.csv 增加文件大小

java - 正则表达式 - 如果模式匹配,则替换双引号之间的字符(逗号)

python - Django 模型、添加新值(value)、迁移

python - 通过等待用户按键来暂停循环的最简单方法是什么?

多线程的python请求

python - 如何在python中将json转换为csv

python - 如何检查上传的 N x M csv 文件是否具有某些标题?