[Biopython] Problem using Entrez with Python 3.2

Wibowo Arindrarto w.arindrarto at gmail.com
Sat Dec 15 05:58:44 UTC 2012


Hi Nicolas,

AFAIK, the Entrez XML parser requires bytes instead of strings. The
distinction matters in Python 3, so what you need to do is to convert
your string stream into a byte stream. To do so, you need to add a new
import:

from io import BytesIO

and replace the last line with these:

bytehandle = BytesIO(bytes(handle.read(), 'utf-8'))  # utf-8 or any
other encoding you want
record = Entrez.read(bytehandle)

We're basically reading the string, convert it to bytes, and create a
buffered bytes I/O that Entrez.read expects.

Alternativey, you can also read the handle into a file first, and open
it in binary mode:

with open('outfile.xml', 'w') as outfile:
    outfile.write(handle.read())

with open('outfile.xml', 'rb') as sourcefile:
    record = Entrez.read(sourcefile)

Hope that helps :),
Bow

On Tue, Dec 11, 2012 at 2:15 AM, Nicolas Joannin
<nicolas.joannin at gmail.com> wrote:
> Hello,
>
> I'm having problems when trying to use Bio.Entrez with Python 3.2.
> I get the following error message:
>
>>>> from Bio import Entrez
>>>> Entrez.email='my at email.address'
>>>> handle=Entrez.esearch(db='nuccore',term='36329')
>>>> record=Entrez.read(handle)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File
> "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/Bio/Entrez/__init__.py",
> line 351, in read
>     record = handler.read(handle)
>   File
> "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/Bio/Entrez/Parser.py",
> line 169, in read
>     self.parser.ParseFile(handle)
> TypeError: read() did not return a bytes object (type=str)
>
> Any comment, suggestion or help would be greatly appreciated!
> Best regards,
>
> Nicolas
> _______________________________________________
> Biopython mailing list  -  Biopython at lists.open-bio.org
> http://lists.open-bio.org/mailman/listinfo/biopython



More information about the Biopython mailing list