Is it easy to read a line from a gz-compressed text file using python without extracting the file completely? I have a text.gz file which is aroud 200mb. When I extract it, it becomes 7.4gb. And this is not the only file I have to read. For the total process, I have to read 10 files. Although this will be a sequential job, I think it will a smart thing to do it without extarcting the whole information. I do not even know that it is possible. How can it be done using python? I need to read a text file line-by-line.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Have you tried using gzip.GzipFile? Arguments are similar to
open
.You could use the standard gzip module in python. Just use:
to open the file as any other file and read its lines.
More information here: Python gzip module
Using gzip.GzipFile:
Note:
gzip.open(filename, mode)
is an alias forgzip.GzipFile(filename, mode)
. I prefer the former, as it looks similar towith open(...) as f:
used for opening uncompressed files.