2016-07-25 77 views
0

我想解析一個XML文件到Python列表。我在本網站和其他網站上查看了一些解決方案,但無法讓它們適用於我。我設法做到了,但是以一種對我來說似乎很愚蠢的辛苦方式。看來應該有一個更簡單的方法。如何解析XML文件到列表?

我試圖適應其他人的代碼,以適應我的需求,但這是行不通的,因爲我並不總是肯定我在讀什麼。

這是XML文件:

<?xml version="1.0"?> 
<configuration> 
    <location name ="location"> 
     <latitude>54.637348</latitude> 
     <latHemi>N</latHemi> 
     <longitude>5.829723</longitude> 
     <longHemi>W</longHemi> 
    </location> 
    <microphone name="microphone"> 
     <sensitivity>-26.00</sensitivity> 
    </microphone> 
    <weighting name="weighting"> 
     <cWeight>68</cWeight> 
     <aWeight>2011</aWeight> 
    </weighting> 
    <optionalLevels name="optionalLevels"> 
     <L95>95</L95> 
     <L90>90</L90> 
     <L50>50</L50> 
     <L10>10</L10> 
     <L05>05</L05> 
     <fmax>fmax</fmax> 
    </optionalLevels> 
    <averagingPeriod name="averagingPeriod"> 
     <onemin>1</onemin> 
     <fivemin>5</fivemin> 
     <tenmin>10</tenmin> 
     <fifteenmin>15</fifteenmin> 
     <thirtymin>30</thirtymin> 
    </averagingPeriod> 
    <timeWeighting name="timeWeighting"> 
     <fast>fast</fast> 
     <slow>slow</slow> 
    </timeWeighting> 
    <rebootTime name="rebootTime"> 
     <midnight>midnight</midnight> 
     <sevenAm>7am</sevenAm> 
     <sevenPm>7pm</sevenPm> 
     <elevenPm>23pm</elevenPm> 
    </rebootTime> 
    <remoteUpload name="remoteUpload"> 
     <nointernet>nointernet</nointernet> 
     <vodafone>vodafone</vodafone> 
    </remoteUpload> 
</configuration> 

這是Python程序。

#!/usr/bin/python 
import xml.etree.ElementTree as ET 


import os 
try: 
    import cElementTree as ET 
except ImportError: 
    try: 
     import xml.etree.cElementTree as ET 
    except ImportError: 
     exit_err("Failed to import cElementTree from any known place") 

file_name = ('/home/mark/Desktop/Practice/config_settings.xml') 
full_file = os.path.abspath(os.path.join('data', file_name)) 

dom = ET.parse(full_file) 

tree = ET.parse(full_file) 
root = tree.getroot() 

location_settings = dom.findall('location') 
mic_settings = dom.findall('microphone') 
weighting = dom.findall('weighting') 
olevels = dom.findall('optionalLevels') 
avg_period = dom.findall('averagingPeriod') 
time_weight = dom.findall('timeWeighting') 
reboot = dom.findall('rebootTime') 
remote_upload = dom.findall('remoteUpload') 

for i in location_settings: 

    latitude = i.find('latitude').text 
    latHemi = i.find('latHemi').text 
    longitude = i.find('longitude').text 
    longHemi = i.find('longHemi').text 


for i in mic_settings: 
    sensitivity = i.find('sensitivity').text 

for i in weighting: 
    cWeight = i.find('cWeight').text 
    aWeight = i.find('aWeight').text 

for i in olevels: 
    L95 = i.find('L95').text 
    L90 = i.find('L90').text 
    L50 = i.find('L50').text 
    L10 = i.find('L10').text 
    L05 = i.find('L05').text 

for i in avg_period: 
    onemin = i.find('onemin').text 
    fivemin = i.find('fivemin').text 
    tenmin = i.find('tenmin').text 
    fifteenmin = i.find('fifteenmin').text 
    thirtymin = i.find('thirtymin').text 

for i in time_weight: 
    fast = i.find('fast').text 
    slow = i.find('slow').text 

for i in reboot: 
    midnight = i.find('midnight').text 
    sevenAm = i.find('sevenAm').text 
    sevenPm = i.find('sevenPm').text 
    elevenPm= i.find('elevenPm').text 

for i in remote_upload: 
    nointernet = i.find('nointernet').text 
    vodafone = i.find('vodafone').text 

config_list = [latitude,latHemi,longitude,longHemi,sensitivity,aWeight,cWeight, 
       L95,L90,L50,L10,L05,onemin,fivemin,tenmin,fifteenmin,thirtymin, 
       fast,slow,midnight,sevenAm,sevenAm,elevenPm,nointernet,vodafone] 
print(config_list) 
+0

歡迎來到StackOverflow。你能描述一下你想從XML中解析出的預期輸出嗎?你可以編輯你的問題以包括那 –

+0

你期待什麼樣的清單?頂級元素的單一列表?列表清單? –

回答

2

您提出的問題不是很好定義。 XML結構與開始時的列表結構不一致。如果您是python的新手,我認爲最好的方法是使用類似xmltodict的東西,它會將xml中的隱式模式解析爲python數據結構。

例如

import xmltodict 
xml = """<?xml version="1.0"?> 
<configuration> 
    <location name ="location"> 
     <latitude>54.637348</latitude> 
     <latHemi>N</latHemi> 
     <longitude>5.829723</longitude> 
     <longHemi>W</longHemi> 
    </location> 
    <microphone name="microphone"> 
     <sensitivity>-26.00</sensitivity> 
    </microphone> 
    <weighting name="weighting"> 
     <cWeight>68</cWeight> 
     <aWeight>2011</aWeight> 
    </weighting> 
    <optionalLevels name="optionalLevels"> 
     <L95>95</L95> 
     <L90>90</L90> 
     <L50>50</L50> 
     <L10>10</L10> 
     <L05>05</L05> 
     <fmax>fmax</fmax> 
    </optionalLevels> 
    <averagingPeriod name="averagingPeriod"> 
     <onemin>1</onemin> 
     <fivemin>5</fivemin> 
     <tenmin>10</tenmin> 
     <fifteenmin>15</fifteenmin> 
     <thirtymin>30</thirtymin> 
    </averagingPeriod> 
    <timeWeighting name="timeWeighting"> 
     <fast>fast</fast> 
     <slow>slow</slow> 
    </timeWeighting> 
    <rebootTime name="rebootTime"> 
     <midnight>midnight</midnight> 
     <sevenAm>7am</sevenAm> 
     <sevenPm>7pm</sevenPm> 
     <elevenPm>23pm</elevenPm> 
    </rebootTime> 
    <remoteUpload name="remoteUpload"> 
     <nointernet>nointernet</nointernet> 
     <vodafone>vodafone</vodafone> 
    </remoteUpload> 
</configuration>""" 
d = xmltodict.parse(xml) 
0

感謝您的意見。對不起,如果問題不是很好。我自己找到了答案。我正在尋求將XML子元素解析到列表中,以便以後在另一個程序中使用。我想到了。感謝您的耐心等待。