在 Python 中将 gz 文件读取为字符串

Read gz file as a string in Python

我一直在寻找一种可以读取 python 中的 gz 文件的方法,我做了类似

的事情
with gzip.open(filepath) as infile:
    ...

但是,读入的数据好像是byte-like,我不能做类似for l in infile的事情。有办法解决吗?

mode='rt'(文本模式)传递给 gzip.open 调用。

来自文档(以上链接):

The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x' or 'xb' for binary mode, or 'rt', 'at', 'wt', or 'xt' for text mode. The default is 'rb'.