Python:从标准输入读取 gzip

如何从标准输入中逐行读取压缩内容?

我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


慕婉清6462132
浏览 183回答 1
1回答

ITMISS

您想打开sys.stdin.buffer,而不是sys.stdin,因为后者透明地将字节解码为字符串。这对我有用:with gzip.open(sys.stdin.buffer, mode='rt') as f:&nbsp; &nbsp; for line in f:&nbsp; &nbsp; &nbsp; &nbsp; print(line)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python