Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use cStringIO the same as StringIO?

Tags:

python

c

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?

like image 796
TIMEX Avatar asked Jan 13 '11 06:01

TIMEX


People also ask

What is StringIO used for?

The StringIO module is an in-memory file-like object. This object can be used as input or output to the most function that would expect a standard file object. When the StringIO object is created it is initialized by passing a string to the constructor. If no string is passed the StringIO will start empty.

What is BytesIO Python?

StringIO and BytesIO are methods that manipulate string and bytes data in memory. StringIO is used for string data and BytesIO is used for binary data. This classes create file like object that operate on string data. The StringIO and BytesIO classes are most useful in scenarios where you need to mimic a normal file.


1 Answers

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)
like image 187
Mark Byers Avatar answered Oct 22 '22 15:10

Mark Byers