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


Groups > comp.lang.python > #71742 > unrolled thread

Re: bz2.decompress as file handle

Started byVincent Davis <vincent@vincentdavis.net>
First post2014-05-18 20:38 -0600
Last post2014-05-18 20:38 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#71742 — Re: bz2.decompress as file handle

FromVincent Davis <vincent@vincentdavis.net>
Date2014-05-18 20:38 -0600
SubjectRe: bz2.decompress as file handle
Message-ID<mailman.10120.1400467110.18130.python-list@python.org>

[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
>
>
>
>

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web