从python中的gzip文件中读取

时间:2023-02-06 10:56:13

I've just make excises of gzip on python.

我只是在python上删除了gzip。

import gzip
f=gzip.open('Onlyfinnaly.log.gz','rb')
file_content=f.read()
print file_content

And I get no output on the screen. As a beginner of python, I'm wondering what should I do if I want to read the content of the file in the gzip file. Thank you.

我在屏幕上没有输出。作为python的初学者,我想知道如果我想在gzip文件中读取文件的内容该怎么办。谢谢。

2 个解决方案

#1


38  

Try gzipping some data through the gzip libary like this...

尝试通过像这样的gzip库gzipping一些数据...

import gzip
content = "Lots of content here"
f = gzip.open('Onlyfinnaly.log.gz', 'wb')
f.write(content)
f.close()

... then run your code as posted ...

...然后运行您的代码发布...

import gzip
f=gzip.open('Onlyfinnaly.log.gz','rb')
file_content=f.read()
print file_content

This method worked for me as for some reason the gzip library fails to read some files.

这个方法对我有用,因为某些原因gzip库无法读取某些文件。

#2


14  

python: read lines from compressed text files

python:从压缩文本文件中读取行

Using gzip.GzipFile:

import gzip

with gzip.open('input.gz','r') as fin:        
    for line in fin:        
        print('got line', line)

#1


38  

Try gzipping some data through the gzip libary like this...

尝试通过像这样的gzip库gzipping一些数据...

import gzip
content = "Lots of content here"
f = gzip.open('Onlyfinnaly.log.gz', 'wb')
f.write(content)
f.close()

... then run your code as posted ...

...然后运行您的代码发布...

import gzip
f=gzip.open('Onlyfinnaly.log.gz','rb')
file_content=f.read()
print file_content

This method worked for me as for some reason the gzip library fails to read some files.

这个方法对我有用,因为某些原因gzip库无法读取某些文件。

#2


14  

python: read lines from compressed text files

python:从压缩文本文件中读取行

Using gzip.GzipFile:

import gzip

with gzip.open('input.gz','r') as fin:        
    for line in fin:        
        print('got line', line)