简体   繁体   English

编码元组(内存)以进行导出

[英]encode tuple (memory) for exporting

I have a list with different values. 我有一个具有不同值的列表。 It looks like this: 看起来像这样:

data = [
('Column1', 'Column2'),
('myFirstNovel', 'myAge'),
('mySecondNovel', 'myAge2'),
('myThirdNovel', 'myAge3'),
('myFourthNovel', 'myAge4')
]

I'm getting encoding errors when I'm writing the data to csv and thus want to encode the data before exporting. 将数据写入csv时出现编码错误,因此要在导出之前对数据进行编码。 So I tried this: 所以我尝试了这个:

[[all.encode('utf-8') for all in items] for items in data]

Now this doesn't really solve my problem to begin with (the data gets filled with \\xe2\\x80\\x94\\xc2\\xa0 and other stuff). 现在这并不能真正解决我的问题(数据填充为\\ xe2 \\ x80 \\ x94 \\ xc2 \\ xa0和其他内容)。 But main thing is it takes ages and my python pretty much crashes. 但是主要的是它需要很长时间,并且我的python几乎崩溃了。

Is there a better method or should I just change export method? 有更好的方法还是应该只更改导出方法?

(using csv tool and writerows right now) (现在使用csv工具和writerows)

If you are using python 2.X you can use following unicode_writer class which python suggests in it's documentation: 如果您使用的是python unicode_writer ,则可以使用以下unicode_writer类,该类在python文档中建议:

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)

And in python 3.X you can simply pass your encoding to open function. 在python 3.x中,您可以简单地将编码传递给open函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM