如何从标准输入中逐行读取压缩内容?我a.gz在当前目录中有一个包含 UTF-8 内容的 gzip 文件。场景一:使用gzip.open(filename)作品。我可以打印解压缩的线条。with gzip.open('a.gz', 'rt') as f: for line in f: print(line)# python3 my_script.py场景2:我想从标准输入读取 gzip 压缩的内容。所以我cat将 gzip 压缩文件作为以下脚本的输入。with gzip.open(sys.stdin, mode='rt') as f: for line in f: print(line)# cat a.gz | python3 script.py但是对于方法 2,我收到以下错误:Traceback (most recent call last): File "script.py", line 71, in <module> for line in f: File "....../python3.6/gzip.py", line 289, in read1 return self._buffer.read1(size) File "....../python3.6/_compression.py", line 68, in readinto data = self.read(len(byte_view)) File "....../python3.6/gzip.py", line 463, in read if not self._read_gzip_header(): File "....../python3.6/gzip.py", line 406, in _read_gzip_header magic = self._fp.read(2) File "....../python3.6/gzip.py", line 91, in read self.file.read(size-self._length+read) File "....../python3.6/codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final)UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
1 回答
ITMISS
TA贡献1871条经验 获得超8个赞
您想打开sys.stdin.buffer,而不是sys.stdin,因为后者透明地将字节解码为字符串。这对我有用:
with gzip.open(sys.stdin.buffer, mode='rt') as f:
for line in f:
print(line)
添加回答
举报
0/150
提交
取消