我正在尝试搜索标题中包含特定单词的论文。更准确地说,2010年至2015年期间发表的论文中的病毒或病毒这个词。这是我的代码:
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)
如果我只是打印(记录['TI'],那么我会在搜索查询中获得所有标题的列表。但是,我无法搜索特定的单词。我认为我的错误可能出在“如果title ==“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
from Bio import Medline
handle = Entrez.efetch(db="pubmed", id=pmid_list, rettype="medline", retmode="text")
records = Medline.parse(handle)
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]")
records = Medline.parse(handle)
titles = []
for record in records:
ti_list = record['TI']
for title in ti_list:
titles.append(title)
handle.close()
for title in titles:
print(title)
答案 0 :(得分:1)
如果您想匹配子字符串,请使用中的来查看标题中是否包含任何字词:
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或任何其他示例执行相同操作。