Python3中的StringIO
Python3中的StringIO
本文翻译自:
I am using Python 3.2.1 and I can’t import the
StringIO
module.
我正在使用Python 3.2.1,但无法导入
StringIO
模块。
I use
io.StringIO
and it works, but I can’t use it with
numpy
’s
genfromtxt
like this:
我使用
io.StringIO
并且可以工作,但是不能将其与
numpy
的
genfromtxt
一起使用,如下所示:
x="1 3\n 4.5 8"
numpy.genfromtxt(io.StringIO(x))
I get the following error: 我收到以下错误:
TypeError: Can't convert 'bytes' object to str implicitly
and when I write
import StringIO
it says
当我写
import StringIO
时说
ImportError: No module named 'StringIO'
#1楼
参考:
#2楼
On Python 3
numpy.genfromtxt
expects a bytes stream.
在Python 3上,
numpy.genfromtxt
需要字节流。
Use the following:
使用以下内容:
numpy.genfromtxt(io.BytesIO(x.encode()))
#3楼
when i write import StringIO it says there is no such module. 当我写导入StringIO时,它说没有这样的模块。
From : 从 :
The
StringIO
andcStringIO
modules are gone.StringIO
和cStringIO
模块不见了。 Instead, import theio
module and useio.StringIO
orio.BytesIO
for text and data respectively. 而是导入io
模块,并将io.StringIO
或io.BytesIO
分别用于文本和数据。
. 。
A possibly useful method of fixing some Python 2 code to also work in Python 3 (caveat emptor): 修复一些Python 2代码以使其在Python 3(caveat emptor)中工作的可能有用的方法:
try:
from StringIO import StringIO ## for Python 2
except ImportError:
from io import StringIO ## for Python 3
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. 注意:此示例可能与问题的主要内容相切,仅作为一般性地解决缺少的StringIO
模块时要考虑的StringIO
。 For a more direct solution the the messageTypeError: Can't convert 'bytes' object to str implicitly
, see . 要获得更直接的解决方案,请输入消息TypeError: Can't convert 'bytes' object to str implicitly
,请参见此 。
#4楼
就我而言,我使用了:
from io import StringIO
#5楼
Thank you OP for your question, and Roman for your answer. 谢谢OP的问题,以及Roman的回答。 I had to search a bit to find this; 我不得不花点时间找到它。 I hope the following helps others. 希望以下内容对他人有所帮助。
Python 2.7 Python 2.7
See: 请参阅: :
import numpy as np
from StringIO import StringIO
data = "1, abc , 2\n 3, xxx, 4"
print type(data)
"""
<type 'str'>
"""
print '\n', np.genfromtxt(StringIO(data), delimiter=",", dtype="|S3", autostrip=True)
"""
[['1' 'abc' '2']
['3' 'xxx' '4']]
"""
print '\n', type(data)
"""
<type 'str'>
"""
print '\n', np.genfromtxt(StringIO(data), delimiter=",", autostrip=True)
"""
[[1. nan 2.]
[ 3. nan 4.]]
"""
Python 3.5: Python 3.5:
import numpy as np
from io import StringIO
import io
data = "1, abc , 2\n 3, xxx, 4"
#print(data)
"""
1, abc , 2
3, xxx, 4
"""
#print(type(data))
"""
<class 'str'>
"""
#np.genfromtxt(StringIO(data), delimiter=",", autostrip=True)
# TypeError: Can't convert 'bytes' object to str implicitly
print('\n')
print(np.genfromtxt(io.BytesIO(data.encode()), delimiter=",", dtype="|S3", autostrip=True))
"""
[[b'1' b'abc' b'2']
[b'3' b'xxx' b'4']]
"""
print('\n')
print(np.genfromtxt(io.BytesIO(data.encode()), delimiter=",", autostrip=True))
"""
[[1. nan 2.]
[ 3. nan 4.]]
"""
Aside: 在旁边:
dtype="|Sx", where x = any of { 1, 2, 3, …}: dtype =“ | Sx”,其中 x = {1,2,3,…}中的任何一个:
“The |S1 and |S2 strings are data type descriptors; the first means the array holds strings of length 1, the second of length 2. …” “ | S1 和| S2 字符串是数据类型描述符;第一个表示数组保存长度为 1 的字符串,第二个表示长度为 2…。”
#6 楼
You can use the from the module: 您可以从 模块中使用 :
import six
import numpy
x = "1 3\n 4.5 8"
numpy.genfromtxt(six.StringIO(x))