2017-03-21 13 views
2

最近、Biopythonを使用してPubmedから要約を抽出しました。私のコードは以下のようにのpython3で書かれている :UnicodeDecodeError Biopythonを使用してefetchから要約を取得する

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 

私は抽象を取得したいが、それはわずか3つのまたは4つの抽象を得ることに成功しました。その後、エラーが発生します。

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

handle.read()とは、特定の記号や単語を持つ、これらの抽象的に問題を持つように見えます。私はhandleのクラスを知ってprintを使用しよう:

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

結果は次のとおりです。

<_io.TextIOWrapper encoding='cp950'> 

私はすでに解決のためのページの多くを検索しましたが、それらのどれも機能しません。誰も助けることができますか?

+0

も参照https://github.com/biopython/biopython/issues/1402 – peterjc

答えて

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')) 
関連する問題