2

最近,使用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'>

我已经在很多页面上搜索了解决方案,但没有一个有效。任何人都可以帮忙吗?

4

1 回答 1

0

对我来说,你的代码工作正常。这是您网站上的编码问题。您可以以字节模式打开文件并将文本编码为 utf-8 您可以尝试以下解决方法:

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), "wb")    # Create a TXT file with the name of ID
    f.write(handle.read().encode('utf-8'))
于 2017-03-22T14:38:55.363 回答