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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:01:57  来源:igfitidea点击:

Writing to CSV, getting "Error: need to escape" for a blank string

pythoncsv

提问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 listof integers. I jointhe 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_listmay have anywhere from zero to a whole bunch of numbers in it. If it's an empty list, the joinjust sets formatted_numbersequal 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_liston this row. And there is nothing weird about the formatted_numbersbeing written when this error crops up - I tossed in a printstatement 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

p81is 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 descriptionfor item p81(just a blank string), but there isa descriptionfor 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 descriptionhas |in it, which is the delimiter for your csv as well. Hence, csv is trying to escape it, but cannot since no csv.escapechars 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_ALLinstead of csv.QUOTE_NONEas provide a valid quotechar.

或者您可以引用所有字段,通过使用csv.QUOTE_ALL而不是csv.QUOTE_NONEas 提供一个有效的quotechar.