Can I use cStringIO the same as StringIO?

最后都变了- 提交于 2019-12-20 17:34:47

问题


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?


回答1:


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)



回答2:


Nor can you set attributes on a cStringIO.StringIO instance:

>>> from cStringIO import StringIO
>>> s = StringIO()
>>> s.name = 'myfile'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'cStringIO.StringO' object has no attribute 'name'

Several libraries depend on File-like objects having either a name or content_type attribute, so cStringIO.StringIO does not work in these instances.



来源:https://stackoverflow.com/questions/4677512/can-i-use-cstringio-the-same-as-stringio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!