Search code examples
pythoncstringio

Can I use cStringIO the same as StringIO?


I did this:

import cStringIO.StringIO as StringIO

And I realize I've been using it everywhere. Is that fine? Is it treated the same as StringIO?


Solution

  • They are not the same. cStringIO doesn't correctly handle unicode characters.

    >>> StringIO.StringIO().write(u'\u0080')
    
    >>> cStringIO.StringIO().write(u'\u0080')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128)