【问题标题】:Read gz file as a string in Python在 Python 中将 gz 文件作为字符串读取
【发布时间】:2018-08-30 20:17:48
【问题描述】:

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

with gzip.open(filepath) as infile:
    ...

但是,读入的数据似乎是字节状的,我无法执行for l in infile 之类的操作。有没有办法解决这个问题?

【问题讨论】:

    标签: python python-3.x gzip


    【解决方案1】:

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

    来自文档(上面链接):

    对于二进制模式,模式参数可以是'r', 'rb', 'a', 'ab', 'w', 'wb', 'x''xb',对于文本模式,可以是'rt', 'at', 'wt','xt'。默认为'rb'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多