【问题标题】:How do I get a specific file from a GZIP in Python 2.7?如何从 Python 2.7 中的 GZIP 获取特定文件?
【发布时间】:2018-10-30 02:30:03
【问题描述】:

我需要从 GZIP 获取特定的 CSV 文件。到目前为止,我有:

import gzip
with gzip.open('my-files.zip') as myzip:
        with myzip.open('my-doc.csv') as myfile:
            reader = csv.reader(myfile)

这会引发错误:

AttributeError: 'GzipFile' object has no attribute 'open'

我正在使用 zipfile 模块,但 zip 无法打开:

$ file  my-files.zip
my-files.zip: Zip archive data, at least v2.0 to extract

该文件位于:http://www.rivcoeh.org/yelp

【问题讨论】:

  • Gzip 用于压缩/解压缩单个文件,它不知道档案。在 *nix 系统上,通常使用 tar 来处理存档,然后在生成的存档文件上使用 gzip 或 bzip 之类的压缩器。如果您将my-files.zip 传递给命令行解压缩实用程序,您会收到什么错误消息?
  • 您是否混淆了 zip 和 gzip?
  • 不。我正在使用 zipfile 模块,但不断收到raise BadZipfile, "File is not a zip file" 错误。所以我尝试了 GZIP,它没有抛出任何错误,但是我不能使用它,因为它不处理档案。
  • 好的,让我们停止猜测。你有 Mac;请执行file my-files.zip 并将其输出复制到问题本身中。
  • 而且由于一个 GZIP 格式不能容纳多个文件,所以问题标题的答案是“你没有”。

标签: python gzip


【解决方案1】:

gzip 和 zip 是两个完全不同的东西。您不能使用 gzip 类打开 zip 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2020-10-31
    相关资源
    最近更新 更多