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.l...@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
>
>
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to