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

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


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

Like this:

import cStringIO, codecsbuffer = 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, codecsbuffer = cStringIO.StringIO()wrapper = codecs.getwriter("utf8")(buffer)


StringIO documentation:

Unlike the memory files implemented by the StringIO module, those provided by [cStringIO] are not able to accept Unicode strings that cannot be encoded as plain ASCII strings.

If possible, use StringIO instead of cStringIO.


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)