[Numpy-discussion] fromfile with sys.stdin?

Michael Aye kmichael.aye@gmail....
Fri Oct 19 22:49:16 CDT 2012

Is it possible to use sys.stdin as input for numpy.fromfile?
I can't make it work. This simple example:

import sys
import numpy as np
# I know the stream format, so I skip parsing the header
keys = ['lot','lon','tb','qual']
dt = [(key,'f8') for key in keys]
# skip the header, it has 'end' in the last line
while 'end' not in sys.stdin.readline():
# pass the stdin filepointer to fromfile
arr = np.fromfile(sys.stdin, dtype = dt)
print arr

produces this error:

(develop+)[maye@luna1 ~/src/diviner]$ pread des=~/data/data.des < 
~/data/mydata.txt | python test_fromfile.py
Traceback (most recent call last):
  File "test_fromfile.py", line 10, in <module>
    arr = np.fromfile(sys.stdin, dtype = dt)
IOError: could not seek in file

I also tried to catch the binary output stream into a binary file and 
read that with fromfile without any problem with the exact same method 
like here, skipping over the header and so on, so it should not be any 
problem with the rest of the code.

Any hints welcome!

More information about the NumPy-Discussion mailing list