- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要用 Python 处理一个 .gz 文件。
我将文件名传递到我的 Python 脚本中:
infile = sys.argv[1]
with gzip.open(infile, 'rb') as f:
logfile = f.read()
这给了我:
with gzip.open(infile, 'rb') as f:
AttributeError: GzipFile instance has no attribute '__exit__'
如果我手动 gunzip 我的 .gz 文件,然后将其传递给我的 Python 脚本,一切正常。
logfile = open(infile, 'r').read()
注意:我使用的是 Python 2.6.6(r266:84292,2016 年 8 月 18 日,15:13:37)。我无法在此计算机上更新 Python。如何使用 Python 2.6 处理 gzip 文本文件?
最佳答案
gzip
模块的上下文管理器支持是 issue 3860 .
它在 Python 3.1 alpha 1(在 3.x 行)和 2.7 alpha 1(在 2.x 行)中得到修复。它在 2.6.6 中仍然打开,您在这里使用它。
当然,您可以通过不使用上下文管理器语法来解决这个问题:
import sys, gzip
logfile = gzip.open(sys.argv[1], 'rb').read()
关于python - GzipFile 实例在 '__exit__' block 中使用时没有属性 "with:",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48736692/
我基本上想完全按照 gzip.GzipFile 的文档中的内容进行操作: Calling a GzipFile object’s close() method does not close fileo
想象一下下面的简单脚本: def reader(): for line in open('logfile.log'): # do some stuff here like sp
我将非常感谢您的帮助。我从我的代码的这一部分中收到一条错误消息,如下所述,我想用它来通过 affymetrix 微阵列分析(原始数据链接是 https://www.ncbi.nlm.nih.gov/s
我需要用 Python 处理一个 .gz 文件。 我将文件名传递到我的 Python 脚本中: infile = sys.argv[1] with gzip.open(infile, 'rb') as
我想阅读gzip并更新内容。 但是,我收到错误 'AttributeError: 'GzipFile' object has no attribute 'extrastart''. import gz
在 Python 2.7 (Pandas 0.22.0) 中,将 Pandas 数据帧保存到内存中的压缩 csv 工作方式如下: from io import BytesIO import gzip
这就是我正在做的 sudo gem install rails ERROR: While executing gem ... (Zlib::GzipFile::Error) unexpect
我正在将内容写入内存中的二进制流,以便将内容上传到 S3,而不将其存储在本地文件中(我的内存比磁盘空间多)。以下代码有效,但 mypy mvce.py失败 mvce.py:6: error: Argu
我使用 Bottle 来接收压缩后的请求正文。当请求的大小很小时,一切都很好。但是,如果请求正文的大小稍大(例如,>= 20kb),则会抛出 IOError。 以下是读取并解压请求体的代码: @pos
我正在使用 Python 研究 Stack Overflow API。我正在尝试解码 API 提供的压缩响应。 import urllib, gzip url = urllib.urlopen('ht
我正在开发 Sencha touch 2 应用程序。我一直在关注“Styling the user interface of a Sencha Touch application”关于 secha t
我正在尝试使用以下 Ruby 代码解压缩文件。 File.open("file_compressed.gz") do |compressed| File.open("file_decomp","w
我在运行时收到奇怪的 bundle 程序错误 bundle pack 使用 bundle 器0.9.12 有任何想法吗? (有关更好的格式化代码,请参见pastie:http://pastie.org
我是一名优秀的程序员,十分优秀!