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

后端 未结 3 1376
野性不改
野性不改 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:05

    Then I converted all integers to string,

    You converted both integers and strings to byte strings. For strings this will use the default character encoding which happens to be ASCII, and this fails when you have non-ASCII characters. You want unicode instead of str.

    self.writer.writerow([unicode(s).encode("utf-8") for s in row])
    

    It might be better to convert everything to unicode before calling that method. The class is designed specifically for parsing Unicode strings. It was not designed to support other data types.

提交回复
热议问题