Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71743

Re: bz2.decompress as file handle

References <CALyJZZU9yJpTQZkL77uM+92TdW=UR=4n4oxmMFPXiF4mCr73+g@mail.gmail.com> <20140518213256.5a0f2d71@bigbox.christie.dr> <CALyJZZXyox2XjgBkomXuq0o5259GjwbzWyGeo_MhVrRJ_dGEYg@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2014-05-18 21:44 -0600
Subject Re: bz2.decompress as file handle
Newsgroups comp.lang.python
Message-ID <mailman.10121.1400471096.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, May 18, 2014 at 8:38 PM, Vincent Davis <vincent@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!

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: bz2.decompress as file handle Ian Kelly <ian.g.kelly@gmail.com> - 2014-05-18 21:44 -0600

csiph-web