首页 > 代码库 > AttributeError: type object '_io.StringIO' has no attribute 'StringIO'

AttributeError: type object '_io.StringIO' has no attribute 'StringIO'

python2导入StringIO模块,直接:

from StringIO import StringIO

对于python3,StringIO和cStringIO模块已经没了,如果要使用的话,需要导入io模块:

from io import StringIOdot_data = http://www.mamicode.com/StringIO()

你也可以导入six模块来使用StringIO:

from six import StringIO

from sklearn.externals.six import StringIO  

例子:对于python2和python3都兼容:

try:    from StringIO import StringIOexcept ImportError:    from io import StringIO
 

REF.

[1]http://stackoverflow.com/questions/11914472/stringio-in-python3

[2]https://github.com/pgbovine/OnlinePythonTutor/issues/111

see What’s New In Python 3.0

AttributeError: type object '_io.StringIO' has no attribute 'StringIO'