Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有原始二进制数据的np.ndarray上的Python3管道I/O失败_Python_Numpy_File Io_Pipe - Fatal编程技术网

具有原始二进制数据的np.ndarray上的Python3管道I/O失败

具有原始二进制数据的np.ndarray上的Python3管道I/O失败,python,numpy,file-io,pipe,Python,Numpy,File Io,Pipe,我有一个二进制原始数据文件in.dat存储4个int32值 $ xxd in.dat 00000000: 0100 0000 0200 0000 0300 0000 0400 0000 ................ 我想把它们读入np.ndarray,乘以2,然后用与.dat中的相同的原始二进制格式将它们写出stdout。预期输出如下: $ xxd out.dat 00000000: 0200 0000 0400 0000 0600 0000 0800 0000 .........

我有一个二进制原始数据文件
in.dat
存储4个int32值

$ xxd in.dat 
00000000: 0100 0000 0200 0000 0300 0000 0400 0000  ................
我想把它们读入
np.ndarray
,乘以2,然后用与.dat中的
相同的原始二进制格式将它们写出stdout。预期输出如下:

$ xxd out.dat 
00000000: 0200 0000 0400 0000 0600 0000 0800 0000  ................
代码是这样的

#!/usr/bin/env python3

import sys
import numpy as np

if __name__ == '__main__':
    y = np.fromfile(sys.stdin, dtype='int32')
    y *= 2
    sys.stdout.buffer.write(y.astype('int32').tobytes())
    exit(0)

我发现它在
中可以正常工作,这是因为在中重定向文件时,可以查找stdin(因为它不是TTY或管道,例如,它只是一个给定了FD 1的文件)。尝试使用
cat foo.txt | python3 test.py
vs
python3 test.py调用以下脚本。如果使用
np.frombuffer
,则它应该以两种方式工作:

pipebytes.py

import numpy as np
import sys
print(np.frombuffer(sys.stdin.buffer.read(), dtype=np.int32))
现在,

Juans MacBook Pro:temp juan$xxd testdata.dat
00000000: 0100 0000 0200 0000 0300 0000            ............
Juans MacBook Pro:temp juan$python pipebytes.py

虽然,我怀疑这会复制数据。

@BaileyParker是的,我怀疑:)你是对的,但还是很有趣为什么
numpy.fromfile
需要一个可查找的文件句柄。我希望它能处理任何文件。
$ cat in.dat | python3 test.py >out.dat
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    y = np.fromfile(sys.stdin, dtype='int32')
OSError: obtaining file position failed
import sys

sys.stdin.seek(1)
print(sys.stdin.read())
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    sys.stdin.seek(1)
io.UnsupportedOperation: underlying stream is not seekable
import struct
import sys

FORMAT = '@i'


def main():
    try:
        while True:
            num = struct.unpack(FORMAT, sys.stdin.buffer.read(struct.calcsize(FORMAT)))
            sys.stdout.buffer.write(struct.pack(FORMAT, num * 2))
    except EOFError:
        pass

if __name__ == '__main__':
    main()
import numpy as np
import sys
print(np.frombuffer(sys.stdin.buffer.read(), dtype=np.int32))
Juans-MacBook-Pro:temp juan$ xxd testdata.dat
00000000: 0100 0000 0200 0000 0300 0000            ............
Juans-MacBook-Pro:temp juan$ python pipebytes.py < testdata.dat
[1 2 3]
Juans-MacBook-Pro:temp juan$ cat testdata.dat | python pipebytes.py
[1 2 3]
Juans-MacBook-Pro:temp juan$