Python 写入 CSV,为空白字符串获取“错误:需要转义”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32107790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing to CSV, getting "Error: need to escape" for a blank string
提问by souldeux
I am probably going to feel very dumb when someone spots what I'm doing wrong here, but I am finding myself unable to defeat what looks like it should be a simple error.
当有人发现我在这里做错了时,我可能会感到非常愚蠢,但我发现自己无法击败看起来应该是一个简单错误的东西。
I am writing some data to a CSV with Python. One of the things I want to write is a list
of integers. I join
the list into a string before writing it to the file:
我正在使用 Python 将一些数据写入 CSV。我想写的一件事是list
整数。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
may have anywhere from zero to a whole bunch of numbers in it. If it's an empty list, the join
just sets formatted_numbers
equal to a blank string. If it's not an empty list, I get a string made of up integers connected by hyphens.
number_list
可能有从零到一大堆数字的任何地方。如果它是一个空列表,join
则只设置formatted_numbers
等于一个空字符串。如果它不是空列表,我会得到一个由连字符连接的整数组成的字符串。
number_list = [1,2,34,12]
formatted_numbers = '1-2-34-12'
number_list = []
formatted_numbers = ''
That's the idea, anyway. In reality, what happens is the first five rows write successfully then I get:
反正就是这个想法。实际上,发生的是前五行写入成功然后我得到:
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
Now in this particular situation, the first five rows that write successfully have an empty number_list
. The row that consistently crashes alsohas an empty number_list
. There is nothing weird about the value being written immediately before or after number_list
on this row. And there is nothing weird about the formatted_numbers
being written when this error crops up - I tossed in a print
statement to debug, and it's just an empty string like the five before it.
现在在这种特殊情况下,成功写入的前五行有一个空的number_list
. 持续崩溃的行也有一个空的number_list
. number_list
在此行之前或之后立即写入的值没有什么奇怪的。formatted_numbers
当这个错误出现时,写入并没有什么奇怪的- 我在一个print
语句中加入调试,它只是一个空字符串,就像它之前的五个一样。
Can anyone help me figure out where I might be going wrong here?
谁能帮我弄清楚我在这里可能出错的地方?
Edit: I have added these print statements:
编辑:我添加了这些打印语句:
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
])
The result:
结果:
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
is not written to the CSV - this is where the crash occurs. However, as you can see, print repr(formatted_numbers)
reveals it to be a blank string identical to those before it. There is no description
for item p81
(just a blank string), but there isa description
for the item preceding it.
p81
未写入 CSV - 这是发生崩溃的地方。但是,如您所见,print repr(formatted_numbers)
显示它是一个与之前相同的空白字符串。没有description
对项目p81
(只是一个空字符串),但有是一个description
为它前面的项目。
采纳答案by Anand S Kumar
The issue is most probably occuring because your description
has |
in it, which is the delimiter for your csv as well. Hence, csv is trying to escape it, but cannot since no csv.escapechar
s are set. Example to show same issue in my computer -
这个问题很可能是因为你description
有|
,这也是你的 csv 的分隔符。因此, csv 试图逃避它,但不能,因为没有csv.escapechar
设置 s。在我的计算机中显示相同问题的示例 -
>>> 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
One fix would be to provide an escapechar so that it can be escaped. Example -
一种解决方法是提供一个转义符,以便它可以被转义。例子 -
writer = csv.writer(f, quoting=csv.QUOTE_NONE, delimiter='|', quotechar='',escapechar='\') #Or any other appropriate escapechar
Or another fix would be to remove the |
in the description before trying to write it, if you do not really need it in the description field -
或者另一个解决方法是|
在尝试编写之前删除描述中的 ,如果您在描述字段中并不真正需要它 -
description = description.replace('|','')
Or you can quote all the fields , by using csv.QUOTE_ALL
instead of csv.QUOTE_NONE
as provide a valid quotechar
.
或者您可以引用所有字段,通过使用csv.QUOTE_ALL
而不是csv.QUOTE_NONE
as 提供一个有效的quotechar
.