从文件夹中提取多个xml文件

时间:2019-11-07 09:22:35

标签: python xml sqlite

我正在使用ncbi发布的ftp数据,我必须解析xml

我已将xml文件存储在文件夹中,大约1500(250 gb)文件存储在文件夹中,我正在从文件夹中读取文件,但是在我运行我的代码时,系统被冻结了。如何解决它

import sqlite3 
import os 
import lxml.etree as ET 


path = '/home/shayez/Desktop/Ex'

listfile = []
files = os.listdir(path)
for name in files:

listfile.append(name)


pmdata = []    

for name2 in listfile:
    full_file = os.path.abspath(os.path.join('Ex',name2))
    dom = ET.parse(full_file)
    pmdat = dom.findall('PubmedArticle')
    pmdata.append(pmdat)         


conn = sqlite3.connect('/home/shayez/Desktop/NewNCBI')
co = conn.cursor()
print ("Opened database successfully")

for d in pmdata:
  for c in d :
    PMID = c.find('MedlineCitation/PMID').text
    title = c.find('MedlineCitation/Article/ArticleTitle').text
    Date = 
c.find('MedlineCitation/Article/Journal/JournalIssue/PubDate/Year')
    Date2 = 
c.find('MedlineCitation/Article/Journal/JournalIssue/PubDate/MedlineDate')
    Abstract = c.find('MedlineCitation/Article/Abstract/AbstractText')
    if Abstract is None :
         pass
    elif Date is None:
        example =[(PMID,Date2.text,Abstract.text)]
        co.executemany('INSERT INTO pubdata(PMID,Date,Abstract)  VALUES (?,?,?)',example)
    else:
        example =[(PMID,Date.text,Abstract.text)]
        co.executemany('INSERT INTO pubdata(PMID,Date,Abstract)  VALUES (?,?,?)',example)

conn.commit()
print("Records Save Successfully")
conn.close()

0 个答案:

没有答案