fseek In Compressed Files
Dave Angel
davea at davea.name
Thu Jan 30 06:55:32 EST 2014
Ayushi Dalmia <ayushidalmia2604 at gmail.com> Wrote in message:
> Hello,
>
> I need to randomly access a bzip2 or gzip file. How can I set the offset for a line and later retreive the line from the file using the offset. Pointers in this direction will help.
>
Start with the zlib module. Note that it doesn't handle all
possible compression types, like compress and pack.
I don't imagine that seeking to a line in a compressed text file
would be any easier than a non compressed one. Try using
gzip.open in a text mode to get a handle, then loop through it
line by line. If you save all the offsets in a list, you
should
subsequently be able to seek to a remembered offset. But
realize it'll be horribly slow, compared to a non compressed
one.
Consider using readlines and referencing the lines from there. Or
building a temp file if too big for ram.
If this is not enough, tell us your Python version and your os,
and show what you've tried and what went wrong.
--
DaveA
More information about the Python-list
mailing list