2017-03-21 4 views
2

Vor kurzem mit dem Biopython extrahieren einige Zusammenfassung von Pubmed. Mein Code wird in Python3 geschrieben wie folgt:UnicodeDecodeError mit Biopython zum Abrufen der Zusammenfassung von E-Fetch

from Bio import Entrez 

Entrez.email = "[email protected]" # 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 

I abstrakt bekommen wollen, aber es gelingt nur drei oder vier abstrakt zu bekommen. Dann tritt ein Fehler:

UnicodeDecodeError: 'cp950' codec can't decode byte 0xc5 in position 288: illegal multibyte sequence 

Die handle.read() scheint wie mit Problem mit diesen abstrakten, in denen bestimmte Zeichen oder Worte mit. Ich versuche print zu verwenden, um die Klasse von handle zu wissen:

handle = Entrez.efetch(db="pubmed", id=idlist, rettype="abstract", retmode="text") 
print(handle) 

Das Ergebnis ist:

<_io.TextIOWrapper encoding='cp950'> 

Ich habe schon viele Seiten für die Lösung gesucht, aber keiner von ihnen arbeitet. Kann jemand helfen?

+0

Siehe auch https://github.com/biopython/biopython/issues/1402 – peterjc

Antwort

0

Für mich funktioniert Ihr Code gut. Es ist ein Codierungsproblem auf Ihrer Website. Sie können Datei in Byte-Modus öffnen und den Text in utf-8 kodieren Sie können wie diese versuchen, Abhilfe:

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')) 
Verwandte Themen