2012-08-11 36 views
192

我使用的是Python 3.2.1,我無法導入StringIO模塊。我用 io.StringIO和它的作品,但我不能numpygenfromtxt像這樣使用它:Python3中的StringIO

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

我得到以下錯誤:

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

,當我寫import StringIO它說

ImportError: No module named 'StringIO' 

回答

50

在Python 3上numpy.genfromtxt需要一個字節流。使用以下命令:

numpy.genfromtxt(io.BytesIO(x.encode())) 
342

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 this answer .

+6

值得一提的是這些不一樣,所以你可以結果 與'TypeError's(預期的字符串參數,得到'字節'),如果你孤立地進行此更改。你需要仔細區分python 3中的btyes和str(unicode)。 – 2015-04-22 03:13:47

+5

對於像我這樣的新手來說:從io import StringIO意味着你把它稱爲StringIO(),而不是io.StringIO()。 – Noumenon 2015-07-07 23:43:54

+4

如何實際上與Python 2和3兼容:只是'從io import StringIO' – 2015-08-11 19:35:43

21

在我的情況我已經使用:

from io import StringIO 
11

謝謝你的問題,羅馬你的答案。我不得不尋找一點才能找到它;我希望以下幫助他人。

的Python 2.7

參見:https://docs.scipy.org/doc/numpy-dev/user/basics.io.genfromtxt.html

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:

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.]] 
""" 

旁白:

D型= 「| SX」,其中x =任何的{1,2,3,...}:

dtypes. Difference between S1 and S2 in Python

「的| S1和| S2的字符串是數據類型描述符;第一裝置陣列保持長度1,長度爲2的第二串...」

8

可以使用StringIOsix模塊:

import six 
import numpy 

x = "1 3\n 4.5 8" 
numpy.genfromtxt(six.StringIO(x)) 
-5

嘗試從StringIO的這個

import StringIO

x =「1 3 \ n 4。5 8"

numpy.genfromtxt(StringIO的(X))

3

爲了使實施例從here 工作與Python 3.5.2,可以重寫如下:

import io 
data =io.BytesIO(b"1, 2, 3\n4, 5, 6") 
import numpy 
numpy.genfromtxt(data, delimiter=",") 

原因因爲這個改變可能是文件的內容是數據(字節),這些數據(字節)在解碼之前不會產生文本。genfrombytes可能會比genfromtxt更好。