最近,使用Biopython从 Pubmed 中提取了一些摘要。我的代码是用Python3编写的,如下所示:
from Bio import Entrez
Entrez.email = "myemail@example.com" # Always tell NCBI who you are
def get_number(): #Get the total number of abstract available in Pubmed
handle = Entrez.egquery(term="allergic contact dermatitis ")
record = Entrez.read(handle)
for row in record["eGQueryResult"]:
if row["DbName"]=="pubmed":
return int(row["Count"])
def get_id(): #Get all the ID of the abstract available in Pubmed
handle = Entrez.esearch(db="pubmed", term="allergic contact dermatitis ", retmax=200)
record = Entrez.read(handle)
idlist = record["IdList"]
return idlist
idlist = get_id()
for ids in idlist: #Download the abstract based on their ID
handle = Entrez.efetch(db="pubmed", id=ids, rettype="abstract", retmode="text") # Retmode Can Be txt / json / xml / csv
f = open("{}.txt".format(ids), "w") # Create a TXT file with the name of ID
f.write(handle.read()) #Write the abstract to the TXT file
我想获得200个摘要,但它只能获得三四个摘要。然后,出现错误:
UnicodeDecodeError: 'cp950' codec can't decode byte 0xc5 in position 288: illegal multibyte sequence
handle.read()
似乎对那些具有某些符号或单词的抽象有问题。我尝试使用print
来了解以下类别handle
:
handle = Entrez.efetch(db="pubmed", id=idlist, rettype="abstract", retmode="text")
print(handle)
结果是:
<_io.TextIOWrapper encoding='cp950'>
我已经在很多页面上搜索了解决方案,但没有一个有效。任何人都可以帮忙吗?