2016-11-04 11 views
0

BioPython을 사용하여 PubMed 제목에서 인용에 대한 데이터의 CSV 파일을 채 웁니다. 지금까지이 쓴 :BioPython을 사용하여 PubMed 검색 및 CSV로 작성

import csv 
from Bio import Entrez 
import bs4 

Entrez.email = "my_email" 
CSVfile = open('srData.csv') 
fileReader = csv.reader(CSVfile) 
Data = list(fileReader) 

with open('blank.csv','w') as f1: 
    writer=csv.writer(f1, delimiter='\t',lineterminator='\n',) 
    for id in Data: 
    handle = Entrez.efetch(db="pubmed", id=id, rettype="gb", retmode="xml") 
    record = Entrez.read(handle) 
    title=record[0]['MedlineCitation']['Article']['ArticleTitle'] 
    abstract=record[0]['MedlineCitation']['Article']['Abstract'] 
    mesh =record[0]['MedlineCitation']['MeshHeadingList'] 
    descriptors = ','.join(term['DescriptorName'] for term in mesh) 
    writer.writerow([title, abstract, descriptors]) 

그러나,이 제목, 초록 및 메쉬 용어는 자신의 유형에 따른 인 내가 감히 구분 된 여러 컬럼에 걸쳐 확산되지 않는 특이한 출력을 생성합니다.(). 내 csv 테이블을 제목이 포함 된 세 개의 열과 다른 하나의 추상 및 다른 메쉬 용어로 구성해야합니다.

어떻게하면됩니까?

명확히하기 위해 샘플 출력은, 첫 번째 열은 전체 제목을 포함하고 추상적이고 다음 몇 컬럼의 시작은 추상의 다음 부분을 포함한다. 나는 그것들을 별개의 열로 나누어야한다. 즉. 첫 번째 열은 제목 만 포함해야합니다. 초록은 초록, 세 번째는 MeSH 만 사용합니다.

현재, 첫 번째 열은 포함

"Distinct and combined vascular effects of ACE blockade and HMG-CoA reductase inhibition in hypertensive subjects. {u'AbstractText': ['Hypercholesterolemia and hypertension are frequently associated with elevated sympathetic activity. Both are independent cardiovascular risk factors and both affect endothelium-mediated vasodilation. To identify the effects of cholesterol-lowering and antihypertensive treatments on vascular reactivity and vasodilative capacity" 
+0

당신이 제목 "무엇을 의미합니까를, 추상 및 MeSH 용어는 여러 열에 걸쳐 있습니다. " 샘플 출력을 보여줄 수 있습니까? – larsks

+0

@ 아이스크. – Toby

답변

0

record[0]['MedlineCitation']['Article']['Abstract']의 값은 추상적 인 텍스트와 짧은 요약을 포함하는 사전입니다. 대신 실제 추상, 원하는 경우 :

abstract=record[0]['MedlineCitation']['Article']['Abstract'] 

을 당신이 필요합니다

abstract=record[0]['MedlineCitation']['Article']['Abstract']['AbstractText'][0] 

지금 abstract는 하나의 문자열을 포함하고 CSV 파일에 쓰기에 적합해야한다.

업데이트 같은 입력 데이터를 사용하는 경우에도 나는, 당신은 당신의 코멘트에 설명한 오류를 재현 할 수없는거야

:

>>> from Bio import Entrez 
>>> Entrez.email = '...' 
>>> id=10067800 
>>> handle = Entrez.efetch(db="pubmed", id=id, rettype="gb", retmode="xml") 
>>> record = Entrez.read(handle) 
>>> abstract=record[0]['MedlineCitation']['Article']['Abstract']['AbstractText'][0] 
>>> abstract 
StringElement('To assess the antihypertensive efficacy and safety of the novel AT1 receptor antagonist, telmisartan, compared with that of enalapril in elderly patients with mild to moderate hypertension.', attributes={u'NlmCategory': u'OBJECTIVE', u'Label': u'OBJECTIVE'}) 
>>> 
+0

이 오류가 발생했지만 다음과 같은 오류가 표시되었습니다. Traceback (가장 최근의 마지막 통화) : 파일 "/Users/...Desktop/csvPubMed.py", 줄 16, 개요 = record [0] [ 'MedlineCitation' ] [ 'AbstractText'] KeyError : 'Abstract' – Toby

+0

이 오류는 의미가 없습니다. 'record [0] ['MedlineCitation '] ['Article '] [ 'Abstract']'는 이미 코드에서 사용하고있는 것입니다. 질문에 게시 된 코드가 오류없이 실행되면 내 코드에서 오류가 표시되지 않아야합니다 (데이터가 사용중인 것과 다른 경우 'AbstractText'와 관련된 오류가 표시 될 수 있음). – larsks

+0

첫 번째 열에는 여전히 추상 부분이 포함되어 있습니다. – Toby