【问题标题】:Convert Bio.Entrez class to string将 Bio.Entrez 类转换为字符串
【发布时间】:2016-11-21 00:56:52
【问题描述】:

我目前正在使用 BioPython 从 PMID 查询 PubMed 记录。然后,我将所需信息存储在一个名为 abstract 的变量中,其数据类型为:

class 'Bio.Entrez.Parser.StructureElement'>

例如,如果我打印它,我会收到:

{u'AbstractText': ['Hypercholesterolemia and hypertension are frequently associated with elevated sympathetic activity. Both are independent cardiovascular ris....']}

我希望将方括号之间的文本写入字符串。我怎样才能快速有效地做到这一点?

【问题讨论】:

    标签: python string biopython


    【解决方案1】:

    我不知道您是如何从 PubMed 获取数据的,但如果您需要查询 PubMed 并获取摘要,您可以使用以下 sn-p。

    from Bio import Medline
    from Bio import Entrez
    Entrez.email = 'my@email.com'
    
    #a list of pubmed IDs
    idlist = ['10024335']
    
    #query PubMed
    handle = Entrez.efetch(db="pubmed", id=idlist, rettype="medline", retmode="text")
    records = Medline.parse(handle)
    
    #print the abstract of all records
    for r in records:
        print(r['AB'])
    

    输出

    '高胆固醇血症和高血压经常与

    ...

    高胆固醇血症高血压患者的结构损伤。'

    【讨论】:

      【解决方案2】:

      您应该将其识别为 Python 字典:

      {u'AbstractText': ['Hypercholesterolemia and hypertension are frequently associated with elevated sympathetic activity. Both are independent cardiovascular ris....']}

      这里'AbstractTest' 是一个字典键,['Hypercholesterolemia and hypertension are frequently associated with elevated sympathetic activity. Both are independent cardiovascular ris....'] 是它的关联值。

      注意['Hypercholesterolemia and hypertension are frequently associated with elevated sympathetic activity. Both are independent cardiovascular ris....'] 是一个包含一个字符串的 Python 列表(显示为用点截断)。

      所以像my_dict['AbstractTest'][0] 这样的东西会给你这个列表中的第一个条目。

      【讨论】:

        【解决方案3】:

        由于列表中的某些元素是Bio.Entrez.Parser.StringElement 类型,您可能希望使用内置的 str() 转换为字符串(没有属性和键),如下所示:str(my_dict['AbstractTest'][0])

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-11-12
          • 2011-05-24
          • 1970-01-01
          • 1970-01-01
          • 2011-12-17
          • 2016-12-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多