Biopython类实例-Entrez.read的输出:我不知道如何操作输出

Whe*_*tle 6 python class biopython

我正在尝试从Pubmed下载一些xml-那里没有问题,Biopython很棒。问题是我真的不知道如何操纵输出。我想将大多数已解析的xml放入sql数据库,但是我对输出不熟悉。对于某些事情,我可以像字典一样调用已解析的xml,但对于另一些事情,似乎并不那么简单。

from Bio import Entrez
Entrez.email="xxxxxxxxxxxxx@gmail.com"
import sqlite3 as lite
handle=Entrez.efetch(db='pubmed',id='22737229', retmode='xml')
record = Entrez.read(handle)
Run Code Online (Sandbox Code Playgroud)

如果要查找标题,可以执行以下操作:

title=record[0]['MedlineCitation']['Article']['ArticleTitle']
Run Code Online (Sandbox Code Playgroud)

但是解析对象的类型是一个类:

>>> type(record)
<class 'Bio.Entrez.Parser.ListElement'>
>>>r=record[0]
>>>type(r)
<class 'Bio.Entrez.Parser.DictionaryElement'>
>>> r.keys()
[u'MedlineCitation', u'PubmedData']
Run Code Online (Sandbox Code Playgroud)

这使我认为,必须比将其用作字典要容易得多。但是当我尝试:

>>> r.MedlineCitation

Traceback (most recent call last):
  File "<pyshell#67>", line 1, in <module>
    r.MedlineCitation
AttributeError: 'DictionaryElement' object has no attribute 'MedlineCitation'
Run Code Online (Sandbox Code Playgroud)

没用 我显然可以将其用作字典,但是后来我遇到了问题。

真正的问题是试图像字典一样使用记录时从记录中获取某些信息:

>>> record[0]['MedlineCitation']['PMID']
StringElement('22737229', attributes={u'Version': u'1'})
Run Code Online (Sandbox Code Playgroud)

这意味着我不能只将它(这是一个技术术语;)放入我的sql数据库中,而是需要对其进行转换:

>>> t=record[0]['MedlineCitation']['PMID']
>>> t
StringElement('22737229', attributes={u'Version': u'1'})
>>> int(t)
22737229
>>> str(t)
'22737229'
Run Code Online (Sandbox Code Playgroud)

总而言之,我对Entrez.read()提供的信息深度感到高兴,但是我不确定如何轻松地在结果类实例中使用该信息。通常你可以做这样的事情

record.MedlineCitation
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

干杯

惠顿

Pra*_*ogg 4

Entrez.read()方法将返回一个由ListElements 和DictionaryElements 组成的嵌套数据结构。read有关更多信息,请查看biopython 源中该方法的文档,我将摘录并解释如下:

def read(handle, validate=True):

This function parses an XML file created by NCBI's Entrez Utilities,
returning a multilevel data structure of Python lists and dictionaries.
...
the[se] data structure[s] seem to consist of generic Python lists,
dictionaries, strings, and so on, [but] each of these is actually a class
derived from the base type. This allows us to store the attributes
(if any) of each element in a dictionary my_element.attributes, and
the tag name in my_element.tag.
Run Code Online (Sandbox Code Playgroud)

该包的作者Michiel de Hoon还花了一些时间在源文件的最顶部讨论Parser.py使用ListElement.DictionaryElementEntrez

ListElement如果您非常好奇,您还可以阅读源代码中、DictionaryElement和类的精彩声明StructureElement。我会破坏这个惊喜,只是让您知道它们是基本 Python 数据类型的非常轻量的包装器,并且行为几乎与它们的底层基本数据类型完全相同,除了它们有一个新属性 ,它attributes捕获 XML 属性(键和值)用于正在解析的文档中的每个 XML 节点read

因此,您问题的基本答案是,没有“简单”的方法可以使用点运算符语法来寻址DictionaryElement. 如果你有一个字典元素 d,这样:

>>> d
DictElement({'first_name': 'Russell', 'last_name': 'Jones'}, attributes={'occupation': 'entertainer'})
Run Code Online (Sandbox Code Playgroud)

唯一可以读取的内置方法first_name是使用普通的 python 字典 API,例如:

>>> d['first_name']
'Russell'
>>> d.get('first_name')
'Russell'
>>> d.get('middle_name', 'No Middle Name')
'No Middle Name'
Run Code Online (Sandbox Code Playgroud)

别灰心,这真的没那么糟糕。如果您想获取某些节点并将它们插入到 sqlite 数据库的行中,您只需编写一些小方法,将 DictElement 作为输入,并返回 sqlite 可以接受的内容作为输出。如果您遇到此问题,请随时专门发布另一个与此相关的问题。