Discussion:
[Biopython] [Entrez] bytes objects has no attribute 'read'
c***@posteo.jp
2015-12-19 12:55:29 UTC
Permalink
Don't know what is going on here.
The used zip file has two xml-files with eFetch-records in it.
import Bio.Entrez, zipfile;
z = zipfile.ZipFile('test.zip')
h = z.read(z.namelist()[0])
r = Bio.Entrez.parse(h)
r
<generator object parse at 0xb6c2bcac>
... print(i)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/Bio/Entrez/Parser.py",
line 243, in parse text = handle.read(BLOCK)
AttributeError: 'bytes' object has no attribute 'read'

Any idea?
--
GnuPGP-Key ID 0751A8EC
_______________________________________________
Biopython mailing list - ***@mailman.open-bio.org
http://mailman.open-bio.org/mailman/listinfo/biopython
c***@posteo.jp
2015-12-19 13:28:36 UTC
Permalink
Post by c***@posteo.jp
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/Bio/Entrez/Parser.py",
line 243, in parse text = handle.read(BLOCK)
AttributeError: 'bytes' object has no attribute 'read'
btw. When using zipfile.extractall() and then open these extracted
files with mode 'rb' it workes.
But I don't want to waste resource with storing unnecessary files to
the filesystem.
--
GnuPGP-Key ID 0751A8EC
_______________________________________________
Biopython mailing list - ***@mailman.open-bio.org
http://mailman.open-bio.org/mailman/listinfo/biopython
Joshua Klein
2015-12-19 16:58:52 UTC
Permalink
It looks like what’s happening here is that calling ZipFile.read() returns
bytes instead of a file-like object. To work around this, create an
instance of BytesIO from the result of z.read(), and pass that object to
Entrez.parse. BytesIO converts an in-memory byte string into a file-like
object which supports the interface the parser expects.
​
Post by c***@posteo.jp
Post by c***@posteo.jp
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/Bio/Entrez/Parser.py",
line 243, in parse text = handle.read(BLOCK)
AttributeError: 'bytes' object has no attribute 'read'
btw. When using zipfile.extractall() and then open these extracted
files with mode 'rb' it workes.
But I don't want to waste resource with storing unnecessary files to
the filesystem.
--
GnuPGP-Key ID 0751A8EC
_______________________________________________
http://mailman.open-bio.org/mailman/listinfo/biopython
m***@posteo.org
2015-12-19 22:46:52 UTC
Permalink
Post by c***@posteo.jp
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/Bio/Entrez/Parser.py",
line 243, in parse text = handle.read(BLOCK)
AttributeError: 'bytes' object has no attribute 'read'
Any idea?
_______________________________________________
Biopython mailing list - ***@mailman.open-bio.org
http://mailman.open-bio.org/mailman/listinfo/biopython
Peter Cock
2015-12-19 23:11:07 UTC
Permalink
Post by c***@posteo.jp
Don't know what is going on here.
The used zip file has two xml-files with eFetch-records in it.
import Bio.Entrez, zipfile;
z = zipfile.ZipFile('test.zip')
h = z.read(z.namelist()[0])
Calling z.read(filename) would return the contents of the file in
the zip file as a bytes string.

You need a handle-like object to give to the Biopython parser,
which must support the read method, so:

h = z.open(z.namelist()[0])
for i in Bio.Entrez.parse(h):
print(i)

Is that better?

Peter
_______________________________________________
Biopython mailing list - ***@mailman.open-bio.org
http://mailman.open-bio.org/mailman/listinfo/biopython
c***@posteo.jp
2015-12-20 17:01:52 UTC
Permalink
Post by Peter Cock
You need a handle-like object to give to the Biopython parser,
h = z.open(z.namelist()[0])
print(i)
Great that work.
The open() function is not documented in the zipfile-package.
--
GnuPGP-Key ID 0751A8EC
_______________________________________________
Biopython mailing list - ***@mailman.open-bio.org
http://mailman.open-bio.org/mailman/listinfo/biopython
Peter Cock
2015-12-20 18:50:42 UTC
Permalink
Post by c***@posteo.jp
Post by Peter Cock
You need a handle-like object to give to the Biopython parser,
h = z.open(z.namelist()[0])
print(i)
Great that work.
The open() function is not documented in the zipfile-package.
It is documented, although I did have to search the page carefully to find it:

https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.open
https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.open

Peter
_______________________________________________
Biopython mailing list - ***@mailman.open-bio.org
http://mailman.open-bio.org/mailman/listinfo/biopython

Continue reading on narkive:
Loading...