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


Groups > comp.lang.python > #71742

Re: bz2.decompress as file handle

References <CALyJZZU9yJpTQZkL77uM+92TdW=UR=4n4oxmMFPXiF4mCr73+g@mail.gmail.com> <20140518213256.5a0f2d71@bigbox.christie.dr>
From Vincent Davis <vincent@vincentdavis.net>
Date 2014-05-18 20:38 -0600
Subject Re: bz2.decompress as file handle
Newsgroups comp.lang.python
Message-ID <mailman.10120.1400467110.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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'))

Vincent Davis
720-301-3003


On Sun, May 18, 2014 at 8:32 PM, Tim Chase <python.list@tim.thechases.com>wrote:

> On 2014-05-18 19:53, Vincent Davis wrote:
> > I have a file compressed with bz2 and a function that expects a
> > file handle. When I decompress the bz2 file I get a string (binary)
> > not a file handle.
>
> > from bz2 import decompress,
> >
> > with open('Tests/Affy/affy_v3_ex.CEL.bz2', 'rb') as handle:
> >     cel_data = decompress(handle.read())
>
> When I try (without the Bio.Affy which isn't part of the stdlib), I
> get correct bytes from this:
>
> tim@bigbox:~$ echo hello world > test.txt
> tim@bigbox:~$ bzip2 -9 test.txt
> tim@bigbox:~$ python3
> Python 3.2.3 (default, Feb 20 2013, 14:44:27)
> [GCC 4.7.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from bz2 import decompress
> >>> with open('test.txt.bz2', 'rb') as f:
> ...     data = decompress(f.read())
> ...
> >>> data
> b'hello world\n'
>
>
> > c = CelFile.read(cel_data)
>
> So either you have bad data in the file to begin with, or your
> CelFile.read() function has a bug in it.
>
> -tkc
>
>
>
>

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


Thread

Re: bz2.decompress as file handle Vincent Davis <vincent@vincentdavis.net> - 2014-05-18 20:38 -0600

csiph-web