在Python中,如何使用mox单元测试库模拟在with语句中创建的对象
代码
class MyCode:
def generate_gzip_file(self):
with gzip.GzipFile('file_name.txt.gz','wb') as f:
f.write('data')
单元测试
class MyCodeTest(unittest.TestCase):
def test_generate_gzip_file(self):
mox = mox.Mo
我正在编写一个脚本,它将以gzip流的形式处理来自仪表的数据。在大约90%的情况下,gzip模块工作得很好,但是一些流会导致它生成IOError: Not a gzipped file。如果移除gzip标头,并将泄气流直接输入zlib,则我将得到Error -3 while decompressing data: incorrect header check。在我的头撞在墙上大约半天之后,我发现那些有问题的流包含一个看似随机的额外字节数(这些字节不是gzip数据的一部分)附加在末尾。
令我感到奇怪的是,Python无法处理这些文件有两个原因:
Gzip和7zip都可以在没有问题的情况下打开
我试图在.gz中读取S3文件,总共有120个文件,我想将所有文件合并到一个文件中。
附注:每个文件的大小在(419 to至900 to)之间。
output = []
for i in files:
if i.endswith('.gz'):
obj = s3.Object(bucket_name=bucket ,key=i)
with gzip.GzipFile(fileobj=obj.get()['Body']) as gzipfile:
content = gzipfile.r
我试着读了一个gz文件:
with open(os.path.join(storage_path,file), "rb") as gzipfile:
with gzip.GzipFile(fileobj=gzipfile) as datafile:
data = datafile.read()
它的工作,但我需要的文件名和大小的每个文件包括在我的gz文件。这段代码将包含文件的内容打印到归档中。
如何读取包含在此gz文件中的文件名?
可能重复:
我有一个7GB的gzip syslog文件,它可以提取到超过25 7GB。我只需要检索文件的第一行和最后一行,而不需要立即将整个文件读入内存。
Python2.7中的允许使用读取头部(通过with迭代意味着不必读取整个文件):
>>> from itertools import islice
>>> from gzip import GzipFile
>>> with GzipFile('firewall.4.gz') as file:
... head = list(islice(file,
当安装任何Gem或执行任何与gem gzip相关的列表时,出现如下所示的错误:
C:\Documents and Settings\gangunra>gem install rhosync -v 2.0.0.beta7 --pre
ERROR: While executing gem ... (Zlib::GzipFile::Error)
not in gzip format
C:\Documents and Settings\gangunra>gem list rails -r
*** REMOTE GEMS ***
ERROR: While executin
我希望http将gzip压缩数据从python发布到java,并将其存储为数据库中的BLOB。然后,我想在java中解压缩这个BLOB。因此,我想知道如何在python中发布BLOB,以及如何在java中读取BLOB。下面给出了我的python和java代码。在我的代码中,我将压缩python中的字符串并将压缩后的数据存储在文件中。然后我用java读取该文件并使用GZIPInputStream解压缩它。但我得到了下面的例外。
java.io.IOException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHead
我有一堆json对象需要压缩,因为它占用了太多的磁盘空间,对于其中的几百万,大约20 gigs值。
理想情况下,我想要做的是分别压缩每个,然后当我需要读取它们时,只需迭代加载和解压每个。我尝试通过zlib创建一个文本文件,其中每一行都是一个压缩的json对象,但是这与
decompress error due to a truncated stream,
我相信这是由于包含新行的压缩字符串造成的。
有人知道这么做的好方法吗?
我试图通过以下方式加载mnist数据集:
import keras
from keras.datasets import mnist
(x_train,y_train),(x_test,y_test)=mnist.load_data()
但我知道这个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "E:\anaconda\lib\site-packages\keras\datasets\mnist.py", line
想象一下以下简单的脚本:
def reader():
for line in open('logfile.log'):
# do some stuff here like splitting the line or filtering etc.
yield some_new_line
def writer(stream):
with gzip.GzipFile('some_output_file.gz', 'w') as fh:
for _s in stream:
我正在编写一个实用程序,它接受一个.gz存档,并检查它的内容是否已经存在于指定的文件夹中。如果他们不这样做,它就会在那里提取档案。
我计划这样做的方法是逐个读取.gz存档中文件的文件名,并检查我的目录中是否已经存在这样的文件。但据我所知,用gzip是不可能的。
理想情况下,我要找的是这样的东西:
archive = gzipfile.GzipFile(source)
for i in archive.getmembers():
if os.path.isfile(destination + sep + i.name) and overwrite:
..
我正在从字节中读取gzip文件,我已经从AWS S3加载了这些文件,现在我尝试读取下面的代码:
gzip_bytes = s3.get_file() # for example I have loaded S3
gzip_file = BytesIO(gzip_bytes)
with GzipFile(gzip_file, mode="rb") as file:
# Todo somthing
我得到了以下错误:
Traceback (most recent call last):
...
with GzipFile(BytesIO(pre_fil
这就是我要做的
sudo gem install rails
ERROR: While executing gem ... (Zlib::GzipFile::Error)
unexpected end of file
alex@ubuntu:~/Documents/$ sudo gem fetch -V rails
GET http://rubygems.org/latest_specs.4.8.gz
302 Moved Temporarily
GET http://production.s3.rubygems.org/latest_specs.4.8.gz
200 OK
ERROR