numpy.genfromtxt with Python3 - howto

Helmut Jarausch jarausch at skynet.be
Fri Apr 6 04:33:37 EDT 2012


Hi

I have a machine with a non-UTF8 local.
I can't figure out how to make numpy.genfromtxt work

I pipe some ascii data into the following script but get this
bytes to str hell.

Many thanks for a hint,
Helmut.


#!/usr/bin/python3
import numpy as np
import io
import sys

inpstream = io.open(sys.stdin.fileno(), "r", encoding='latin1')

data = np.genfromtxt(inpstream)
'''
Traceback (most recent call last):
  File "SimpleMatInp.py", line 8, in <module>
    data = np.genfromtxt(inpstream)
  File "/usr/lib64/python3.2/site-packages/numpy/lib/npyio.py", line 
1274, in genfromtxt
    first_values = split_line(first_line)
  File "/usr/lib64/python3.2/site-packages/numpy/lib/_iotools.py", line 
206, in _delimited_splitter
    line = line.split(self.comments)[0].strip(asbytes(" \r\n"))
TypeError: Can't convert 'bytes' object to str implicitly
'''
print(data)

print(data.dtype)

print(data.shape)



More information about the Python-list mailing list