编程语言
首页 > 编程语言> > Python3中的StringIO

Python3中的StringIO

作者:互联网

我使用的是Python 3.2.1,我无法导入StringIO模块.我用
io.StringIO它可以工作,但我不能像numpy的genfromtxt这样使用它:

x="1 3\n 4.5 8"        
numpy.genfromtxt(io.StringIO(x))

我收到以下错误:

TypeError: Can't convert 'bytes' object to str implicitly  

当我写入导入StringIO时,它说

ImportError: No module named 'StringIO'

解决方法:

when i write import StringIO it says there is no such module.

What’s New In Python 3.0开始:

The StringIO and cStringIO modules are gone. Instead, import the io
module and use io.StringIO or io.BytesIO for text and data
respectively.

.

修复一些Python 2代码的一种可能有用的方法也适用于Python 3(需要注意的话):

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

Note: This example may be tangential to the main issue of the question and is included only as something to consider when generically addressing the missing StringIO module. For a more direct solution the the message TypeError: Can't convert 'bytes' object to str implicitly, see 07001.

标签:python,numpy,python-3-x,io,python-3-2
来源: https://codeday.me/bug/20190911/1804432.html