Python: How to get StringIO.writelines to accept unicode string?

后端 未结 4 1411
予麋鹿
予麋鹿 2020-12-28 15:06

I\'m getting a

UnicodeEncodeError: \'ascii\' codec can\'t encode character u\'\\xa3\' in position 34: ordinal not in range(128)

on a strin

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-28 15:38

    You can also encode your string as utf-8 manually before adding it to the StringIO

    for val in rows:
        if isinstance(val, unicode):
            val = val.encode('utf-8')
    result.writelines(rows)
    

提交回复
热议问题