Python 3中的StringIO我正在使用Python3.2.1,我無法導(dǎo)入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當(dāng)我寫import StringIO上面寫著ImportError: No module named 'StringIO'
3 回答

慕少森
TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個贊
當(dāng)我編寫導(dǎo)入StringIO時,它說沒有這樣的模塊。
從…Python3.0的新特性是什么?:
這個
StringIO
和cStringIO
模塊不見了。相反,導(dǎo)入io
模塊與使用io.StringIO
或io.BytesIO
分別用于文本和數(shù)據(jù)。
.
修復(fù)Python 2代碼的一種可能有用的方法也適用于Python 3(警告讀取器):
try: ????from?StringIO?import?StringIOexcept?ImportError: ????from?io?import?StringIO
注意:此示例可能與問題的主要問題切入,并且僅作為一般性地解決缺失問題時考慮的內(nèi)容而包括在內(nèi)。
StringIO
模塊。對于更直接的解決方案,消息TypeError: Can't convert 'bytes' object to str implicitly

郎朗坤
TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個贊
numpy.genfromtxt
numpy.genfromtxt(io.BytesIO(x.encode()))
添加回答
舉報
0/150
提交
取消