2017-01-15 14 views
0

私はプログラミングとPythonを初めて使います。私はすべての問題のセットとエクササイズをしていますが、自分のプロジェクトに取り組んでいます。良い解決策がありますか、これはDBにデータを解析する方法です。

私は私のプロジェクトでやるべきことのいくつかを詳しく扱っていないので、私は余分な研究をしています。私のコードはうまくいきますが、これを実装する正しい方法であるかどうかはわかりません。

基本的には、サイズが10KB未満の小さなXMLファイルを解析しています。私はファイルをループし、4つのフィールドとその値だけをコピーします。私はその後、さらに操作し、より多くのフィールドを追加するか、データを変更する私のために一時的なsqlite DBにそれらの値を挿入します。

私はXMLファイルを解析してDBを更新する2つの別々の関数を書くのではなく、小さな関数を使って両方を行うことができると思いました。 xmlファイルは非常に小さいので、私はパフォーマンスについて心配していません。

ループが完了すると、必要なデータが見つかるので、私のループでDBを同時に更新します。

以下は現在使用しているコードです。しかし、これを行う方法でないなら、私はそれを使用したくありません。もし私がそれを助けることができれば、悪い習慣を学ばないようにしようとする。

import xml.etree.cElementTree as ET 
import sqlite3 

def parseCT(self): 
    """ 
    Read data from XML File. 
    Parse XML data and insert data into Temp DB file 
    """ 

    # Open XML file so we can access data 
    tree = ET.ElementTree(file='acs.xml') 
    root = tree.getroot() 

    # Create temp DB to store read XML data 
    con = sqlite3.connect('ct_temp.db') 
    cur = con.cursor() 
    cur.execute('''CREATE TABLE XMLTable (
       ID TEXT NOT NULL, Description TEXT, 
       VariableType TEXT, Text TEXT, 
       PRIMARY KEY (ID))''') 
    con.commit() # update and save changes to our temp DB. 

    txt1 = '' 
    txt2 = '' 
    txt3 = '' 
    txt4 = '' 

    # Loop to find the elements we are looking for 
    for elem in tree.iter(): 
     if elem.tag == 'ID': 
      txt1 = elem.text 
     elif elem.tag == 'Description': 
      txt2 = elem.text 
     elif elem.tag == 'VariableType': 
      txt3 = elem.text 
     elif elem.tag == 'Text': 
      txt4 = elem.text 

    cur.execute('INSERT INTO XMLTable VALUES (?, ?, ?, ?)', 
       (txt1, txt2, txt3, txt4)) 
    con.commit() 
    con.close() 
+0

テストしましたか? *動作していますか?*これが改善できると思われる作業コードの場合は、[codereview.se]を参照してください。そうでない場合は、問題を明確にしてください。 – jonrsharpe

+0

こんにちは。はい、それは働いているようです。しかし、私がやっていることが正しいかどうかは分かりません。 –

答えて

1

使用findtext

ありがとう:

cur.execute('INSERT INTO XMLTable VALUES (?, ?, ?, ?)', 
      (root.findtext('ID'), root.findtext('Description'), 
      root.findtext('VariableType'), root.findtext('Text'))) 
+0

ありがとう、それを試しに行くでしょう。 –

0

コードを簡素化するために、あなたが行うことができ、あなたのコードが動作すると仮定すると:

# Initial list to maintain the order of params in `INSERT` statement 
query_params_order = ['ID', 'Description', 'VariableType', 'Text'] 

# `.format` will replace `{}` in query based on the order of params in `list` 
cur.execute('''CREATE TABLE XMLTable (
      {} TEXT NOT NULL, {} TEXT, 
      {} TEXT, {} TEXT, 
      PRIMARY KEY (ID))'''.format(*query_params_order)) 

con.commit() 

# `dict` with tag and text mapping 
params_dict = {elem.tag: elem.text for elem in tree.iter()} 

order_params = [params_dict.get(param, '') for param in query_params_order] 

cur.execute('INSERT INTO XMLTable VALUES (?, ?, ?, ?)', 
      order_params) 

の代わり:

cur.execute('''CREATE TABLE XMLTable (
      ID TEXT NOT NULL, Description TEXT, 
      VariableType TEXT, Text TEXT, 
      PRIMARY KEY (ID))''') 
con.commit() # update and save changes to our temp DB. 

txt1 = '' 
txt2 = '' 
txt3 = '' 
txt4 = '' 

# Loop to find the elements we are looking for 
for elem in tree.iter(): 
    if elem.tag == 'ID': 
     txt1 = elem.text 
    elif elem.tag == 'Description': 
     txt2 = elem.text 
    elif elem.tag == 'VariableType': 
     txt3 = elem.text 
    elif elem.tag == 'Text': 
     txt4 = elem.text 

cur.execute('INSERT INTO XMLTable VALUES (?, ?, ?, ?)', 
      (txt1, txt2, txt3, txt4)) 

あなたのコードと同じように動作しますが、よりクリーンでスケーラブルです。

+0

ありがとうございます。ヘルプをよろしくお願いいたします。これを数回練習します。 –

関連する問題