I try to understand how works numpy.getfromtxt method and io.StringIO. On the officical website(https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.genfromtxt.html#numpy.genfromtxt) I found some examples. Here is one of them:
s = StringIO("1,1.3,abcde")
data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),('mystring','S5')], delimiter=",")
But when I run this code on my computer I get: TypeError: must be str or None, not bytes
Tell me please how to fix it?
The example works for me:
It also works for a byte string:
genfromtxt
works with anything that feeds it lines, so I usually use a list of bytestrings directly (when testing questions):Or with several lines
It used to be that with
dtype=None
,genfromtxt
createdS
strings.NumPy dtype issues in genfromtxt(), reads string in as bytestring
With 1.14, we can control the default string dtype:
https://docs.scipy.org/doc/numpy/release.html#encoding-argument-for-text-io-functions
Now I can generate examples with Py3 strings without producing all those ugly
b'string'
results (but got to remember that not everyone has upgraded to 1.14):