regex 使用biopython解析Bicycle搜索结果中的标题

svgewumm  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(104)

我正在尝试使用biopython搜索标题中包含特定单词的论文。更准确地说,是2010年至2015年期间发表的论文中包含的单词viral或virus。以下是我的代码:

import re
from Bio import Medline

handle = Entrez.esearch(db="pubmed",  # database to search
                    term="2010[Date - Publication]:2015[Date - Publication]"
                    )
record = Entrez.read(handle)
handle.close()

pmid_list = record["IdList"] #list of records

handle = Entrez.efetch(db="pubmed", id=pmid_list, rettype="medline",     retmode="text")
records = Medline.parse(handle)

titles = [] # start with empty list of titles
for record in records:
    ti_list = record['TI'] #titles
    for title in ti_list:
        if title == "virus" and title not in titles: #searching viral/virus
        titles.append(title)

print('Publications with viral or virus in the title:')
for record in records:
    print(" ", title)

字符串
如果我只是打印(record ['TI '],那么我会得到一个包含搜索查询中所有标题的列表。但是,我无法搜索到特定的词。我认为我的错误可能在于“if title ==“virus”(因为显然没有一篇论文的标题会单独使用该词)。
我被卡住了。有没有更好的方法在我查询过的论文标题中搜索这个词?
谢谢.
编辑:用re.search更新了代码(但仍然没有成功)

r = re.compile(r"\bvir(al|us)\b")
titles = set()  # start with empty list of titles
for record in records:
    ti_list = record['TI']  # titles
    for title in ti_list:
        if r.search(title):  #
            titles.add(title)

print('Publications with viral or virus in the title:')
for record in records:
     print(" ", title)


新代码:

import re
from Bio import Medline

handle = Entrez.efetch(db="pubmed", id=pmid_list, rettype="medline", retmode="text", 
                       term="2010[Date - Publication]:2015[Date - Publication]")
titles = []
for record in Medline.parse(handle):
    for title in record['TI']:
        titles.append(title)
handle.close()
for title in titles:
    print(title)

0md85ypi

0md85ypi1#

如果你想匹配子字符串,使用 in 来查看标题中是否包含任何单词:

words  = ("viral","virus")
if any(w in title for w in words) and title not in titles: #

字符串
但你似乎想过滤记录,获得任何包含病毒或病毒的记录标题:

st  = {"viral","virus"}

filtered_records = [ record for record in records if any(w in st for w in record['TI'] )]


如果你想匹配子字符串并使用一个模式,那么你实际上需要把它变成一个正则表达式,"vir(al|us)"只是你代码中的一个字符串:

import re

r = re.compile("vir(al|us)")
filtered_records = [record for record in records if any(r.search(w) for w in record['TI'])]


你自己的循环中的正则表达式会去你的if所在的地方:

import re

r = re.compile(r"vir(al|us)")
if r.search(title) and title not in titles: 
      .......


如果你不想病毒等匹配,那么为你的正则表达式使用一个词边界:

r = re.compile(r"\bvir(al|us)\b")


你还应该把标题设置为一个不能有重复的集合,一个使用你自己代码的工作示例:

r = re.compile(r"\bvir(al|us)\b")
titles = set()  # start with empty list of titles
for record in records:
    ti_list = record['TI']  # titles
    for title in ti_list:
        if r.search(title):  #
            titles.add(title)


它可以成为一个集合的理解:

r = re.compile(r"\bvir(al|us)\b")

titles = {title for record in records for title in record['TI']  if r.search(title)} # titles


因为record['TI']返回的是字符串而不是列表:

r = re.compile(r"\bvir(al|us)\b")
titles = set() 
for record in records:
    title = record['TI']  # title is a str not a list
    if r.search(title):  #
           titles.add(title)


对set comp或任何其他示例执行相同的操作。

相关问题