bz2.decompress as file handle

Ian Kelly ian.g.kelly at gmail.com
Sun May 18 23:44:12 EDT 2014


On Sun, May 18, 2014 at 8:38 PM, Vincent Davis <vincent at vincentdavis.net> wrote:
> Well after posting, I think I figured it out.
> The key is to use StringIO to get a file handle on the string. The fact that
> it is binary just complicates it a little.
>
> with open('Tests/Affy/affy_v3_ex.CEL.bz2', 'rb') as handle:
>     cel_data = StringIO(decompress(handle.read()).decode('ascii'))

You can just use bz2.open:

>>> with bz2.open('test.txt.bz2', 'rt', encoding='ascii') as f:
...     print(f.read())
...
hello!



More information about the Python-list mailing list