I am using Python 3.2.1 and I can't import the StringIO
module. I use
io.StringIO
and it works, but I can't use it with numpy
's genfromtxt
like this:
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
ImportError: No module named 'StringIO'
On Python 3
numpy.genfromtxt
expects a bytes stream. Use the following:Thank you OP for your question, and Roman for your answer. I had to search a bit to find this; I hope the following helps others.
Python 2.7
See: https://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html
Python 3.5:
Aside:
dtype="|Sx", where x = any of { 1, 2, 3, ...}:
dtypes. Difference between S1 and S2 in Python
"The |S1 and |S2 strings are data type descriptors; the first means the array holds strings of length 1, the second of length 2. ..."