Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

numpy.genfromtxt with Python3 - howto

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jarausch at skynet

Apr 6, 2012, 1:33 AM

Post #1 of 2 (198 views)
Permalink
numpy.genfromtxt with Python3 - howto

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)
--
http://mail.python.org/mailman/listinfo/python-list


usenet at solar-empire

Apr 6, 2012, 3:03 PM

Post #2 of 2 (187 views)
Permalink
Re: numpy.genfromtxt with Python3 - howto [In reply to]

Helmut Jarausch <jarausch [at] skynet> wrote:
> I have a machine with a non-UTF8 local.
> I can't figure out how to make numpy.genfromtxt work
[...]
> #!/usr/bin/python3
> import numpy as np
> import io
> import sys
>
> inpstream = io.open(sys.stdin.fileno(), "r", encoding='latin1')
>
> data = np.genfromtxt(inpstream)

Hi Helmut, numpy.genfromtxt wants bytes, not str. Use
inpstream = io.open(sys.stdin.fileno(), "rb")
or simply
inpstream = sys.stdin.buffer

Ciao
Marc
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.