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

Joh*_*. M 4 python gzip python-3.x

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

with gzip.open(filepath) as infile:
    ...
Run Code Online (Sandbox Code Playgroud)

然而,读入的数据似乎是字节状的,我不能做类似的事情for l in infile。有办法解决这个问题吗?

Joe*_*don 6

mode='rt'(文本模式)传递给呼叫gzip.open

从文档(上面链接):

模式参数可以是二进制模式或文本模式中的任何一个'r', 'rb', 'a', 'ab', 'w', 'wb', 'x'。默认为.'xb''rt', 'at', 'wt','xt''rb'