0

写入文件时出现此错误。我该如何处理。

Traceback (most recent call last):
  File "C:\Python27\AureusBAXProjectFB.py", line 278, in <module>
    rows = [[unicode(x) for x in row] for row in outlist]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)
>>> 

写入文件的代码

class UnicodeWriter:
    """
    A CSV writer which will write rows to CSV file "f",
    which is encoded in the given encoding.
    """

    def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
        # Redirect output to a queue
        self.queue = cStringIO.StringIO()
        self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
        self.stream = f
        self.encoder = codecs.getincrementalencoder(encoding)()

    def writerow(self, row):
        self.writer.writerow([s.encode("utf-8") for s in row])
        # Fetch UTF-8 output from the queue ...
        data = self.queue.getvalue()
        data = data.decode("utf-8")
        # ... and reencode it into the target encoding
        data = self.encoder.encode(data)
        # write to the target stream
        self.stream.write(data)
        # empty queue
        self.queue.truncate(0)

    def writerows(self, rows):
        for row in rows:
            self.writerow(row)

with open('C:/Users/Desktop/fboutput.csv', 'wb') as f:
    writer = UnicodeWriter(f)
    rows = [[unicode(x) for x in row] for row in outlist]
    writer.writerows(rows)

我正在使用 BeautifulSoup 来解析 html 数据,并且工作正常。仅在写入文件时出现错误。

4

1 回答 1

0

unicode() 构造函数定义为unicode(string[, encoding, errors]),编码默认为 ascii。如果多字节字符串在 outlist 中,您应该指定 unicode 编码,如 utf-8。

于 2013-09-04T09:33:16.890 回答