写入CSV,获取“错误:需要转义”为空白字符串 [英] Writing to CSV, getting "Error: need to escape" for a blank string

查看:3069
本文介绍了写入CSV,获取“错误:需要转义”为空白字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能会觉得很蠢,当有人发现我在这里做错了,但我发现自己无法打败,看起来像是一个简单的错误。

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.

我使用Python写一些数据到CSV。我想写的一个事情是一个列表的整数。

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:

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 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 = [1,2,34,12]
formatted_numbers = '1-2-34-12'

number_list = []
formatted_numbers = ''

这是个想法。实际上,发生的是前5行写成功然后我得到:

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

现在在这种特殊情况下,成功写入的前五行有一个空 number_list 。始终崩溃的行有一个空的 number_list 。在此行上的 number_list 之前或之后写入的值是没有什么奇怪的。并且没有什么奇怪的是,当这个错误恢复时,我写了 formatted_numbers - 我抛出一个打印语句调试

Now in this particular situation, the first five rows that write successfully have an empty number_list. The row that consistently crashes also has 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.

任何人都可以帮我找出我在这里可能会出错的地方吗?

Can anyone help me figure out where I might be going wrong here?

编辑:我添加了以下打印语句:

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
            ])

结果:

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 (只是一个空白字符串)没有描述,但对其前面的项使用描述

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 is a description for the item preceding it.

推荐答案

问题很可能发生,因为您的描述 c $ c> | ,它也是csv的分隔符。因此,csv试图逃避它,但不能,因为没有设置 csv.escapechar 。在我的计算机中显示相同问题的示例 -

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.escapechars are set. Example to show same issue in my computer -

>>> 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,以便它可以逃脱。示例 -

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('|','')


$ b b

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

Or you can quote all the fields , by using csv.QUOTE_ALL instead of csv.QUOTE_NONE as provide a valid quotechar .

这篇关于写入CSV,获取“错误:需要转义”为空白字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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