python csv unicode 'ascii' codec can't encode character u'\xf6' in position 1: ordinal not in range(128)

后端 未结 3 1374
野性不改
野性不改 2020-12-23 16:57

I have copied this script from [python web site][1] This is another question but now problem with encoding:

import sqlite3
import csv
import codecs
import cS         


        
3条回答
  •  太阳男子
    2020-12-23 17:14

    If you are using Python 2:

    make encoding as : str(s.encode("utf-8")) i.e.

    def writerow(self, row):
        self.writer.writerow([str(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)
    

提交回复
热议问题