[Biopython] [Entrez.efetch] save records

Peter Cock p.j.a.cock at googlemail.com
Fri Dec 4 08:33:58 UTC 2015


On Fri, Dec 4, 2015 at 2:28 AM,  <c.buhtz at posteo.jp> wrote:
> On 2015-12-03 23:58 Peter Cock <p.j.a.cock at googlemail.com> wrote:
>> Hmm. I would keep the data as the original XML
>
> How can I receive this in Python3?
> The 'handle' returned from Enterz.efetch() is only a
> "ugly_handle_hack-blabla".

(Slightly off topic, do you care about Python 3.3? Maybe we
can drop support for that and get ride of this ugly-handle-hack
workaround)

Thet returned object is just a (wrapper for) a normal handle,
so you can do handle.read() etc. Typically you would save
it to a file using something like this:

with Entrez.efetch(....) as results_handle:
    with open("saved.xml", "w") as output_handle:
        output_handle.write(results_handle.read())

Or, the more old fashioned with without the with statements:

results_handle = Entrez.efetch(....)
output_handle = open("saved.xml", "w")
output_handle.write(results_handle.read())
output_handle.close()
results_handle.close()

There are some examples in the Entrez chapter for the
Tutorial,

http://biopython.org/DIST/docs/tutorial/Tutorial.html

Peter


More information about the Biopython mailing list