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

后端 未结 4 1410
予麋鹿
予麋鹿 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:35

    You can wrap the StringIO object in a codecs.StreamReaderWriter object to automatically encode and decode unicode.

    Like this:

    import cStringIO, codecs
    buffer = cStringIO.StringIO()
    codecinfo = codecs.lookup("utf8")
    wrapper = codecs.StreamReaderWriter(buffer, 
            codecinfo.streamreader, codecinfo.streamwriter)
    
    wrapper.writelines([u"list of", u"unicode strings"])
    

    buffer will be filled with utf-8 encoded bytes.

    If I understand your case correctly, you will only need to write, so you could also do:

    import cStringIO, codecs
    buffer = cStringIO.StringIO()
    wrapper = codecs.getwriter("utf8")(buffer)
    

提交回复
热议问题