Reading lines of text from 7z compressed files in Python

srinivas devaki mr.eightnoteight at gmail.com
Wed Dec 9 14:12:41 EST 2015


On Dec 9, 2015 3:07 PM, "Anmol Dalmia" <dalmia.anmol at gmail.com> wrote:
>
>
> I wish to use the native LZMA library of Python 3.4 for faster performance
> than any other third- party packages. Is it possible to do so?
>

you can check the source of lzma module main compression and decompression
algorithms were written in c. so obviously you will get faster performance.
In [21]: import _lzma

In [22]: _lzma.__file__
Out[22]:
'/home/eightnoteight/.anaconda3/envs/snakes3.4.3/lib/python3.4/lib-dynload/_
lzma.cpython-34m.so'


and regarding your problem, here's a simple example on how you can read
line by line of your compressed 7z text file.


import lzma
with lzma.open('test.7z', 'w') as lf:
    lf.write(b'123\n'*1000)
with lzma.open('test.7z', 'r') as lf:
    arr = list(lf)
print(len(arr))
print(set(arr))
print(arr[0])
print(arr[0].decode('utf-8'))

[gist] https://gist.github.com/38681cad88928b089abb

later you can even extract that test.7z with 7z command line client with
(7z x test.7z)



More information about the Python-list mailing list