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


Groups > comp.lang.python > #71743

Re: bz2.decompress as file handle

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'string.': 0.05; 'binary': 0.07; 'subject:file': 0.07; 'subject:skip:b 10': 0.07; "'rb')": 0.16; 'posting,': 0.16; 'stringio': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'file': 0.32; 'received:google.com': 0.35; 'handle': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:o 30': 0.61; 'subject:handle': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=bM69UKS0K9cM1dLTVOgt0Q5E6qd36RkjWHZ0Vh4vHjk=; b=em0vjlXTrn4oPLoizeV5bVp0A/6Ehy2Ogd+n0fK2sy3vjoeoqda79THfB7KCUIyJac Ip0RYT9XLl6bVP6VK4lWykEsSQG6dettNA+f3Mhr3qRMWRqxF4q5MFWBW2tkY6Opa3SZ zcVCkCLaIbRN+5JwuUxzfZfbaf7RIWESo7uvymhLuRcj4sDOU6WGY7iA7kGIe5n2DNiH Qb1/PmK9EfuVBxKeSkJvpmxHpPEfA0yr3paZUHij50Y7mcaqv15qTFZFL71z3yqznTS6 nzqSYhTzQN31TA+QQYH09oeTTLglIaX9GKWgV8BIjr/AYw36UDYU5sMj+s96M1XwzZSc 5ZmQ==
X-Received by 10.68.249.2 with SMTP id yq2mr39500786pbc.70.1400471093105; Sun, 18 May 2014 20:44:53 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CALyJZZXyox2XjgBkomXuq0o5259GjwbzWyGeo_MhVrRJ_dGEYg@mail.gmail.com>
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 Sun, 18 May 2014 21:44:12 -0600
Subject Re: bz2.decompress as file handle
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10121.1400471096.18130.python-list@python.org> (permalink)
Lines 14
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1400471096 news.xs4all.nl 2925 [2001:888:2000:d::a6]:41992
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:71743

Show key headers only | 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