我从网站下载了一个示例XML数据,并希望用Python阅读。样本如下:
<Locations>
<Location elevation="933.0" id="3072" latitude="56.879" longitude="-3.42" name="Cairnwell" nationalPark="Cairngorms National Park" region="ta" unitaryAuthArea="Perth and Kinross" />
<Location elevation="134.0" id="3088" latitude="56.852" longitude="-2.264" name="Inverbervie" region="gr" unitaryAuthArea="Aberdeenshire" />
<Location elevation="4.0" id="3094" latitude="57.698" longitude="-2.121" name="Rosehearty Samos" region="gr" unitaryAuthArea="Aberdeenshire" />
<Location elevation="35.0" id="3144" latitude="56.326" longitude="-3.729" name="Strathallan" region="ta" unitaryAuthArea="Perth and Kinross" />
<Location elevation="277.0" id="3152" latitude="55.862" longitude="-3.875" name="Salsburgh" region="st" unitaryAuthArea="North Lanarkshire" />
<Location elevation="57.0" id="3166" latitude="55.928" longitude="-3.343" name="Edinburgh/Gogarbank" region="dg" unitaryAuthArea="Edinburgh" />
<Location elevation="16.0" id="3204" latitude="54.0849" longitude="-4.6321" name="Ronaldsway" region="nw" />
<Location elevation="124.0" id="3210" latitude="54.5181" longitude="-3.615" name="St. Bees Head" region="nw" unitaryAuthArea="Cumbria" />
<Location elevation="28.0" id="3220" latitude="54.933" longitude="-2.963" name="Carlisle" region="nw" unitaryAuthArea="Cumbria" />
<Location elevation="285.0" id="3224" latitude="55.05" longitude="-2.553" name="Spadeadam" region="nw" unitaryAuthArea="Cumbria" />
<Location elevation="32.0" id="3257" latitude="54.296" longitude="-1.53" name="Leeming" region="yh" unitaryAuthArea="North Yorkshire" />
<Location elevation="33.0" id="3261" latitude="54.134" longitude="-1.414" name="Dishforth Airfield" region="yh" unitaryAuthArea="North Yorkshire" />
<Location elevation="7.0" id="3382" latitude="53.867" longitude="-0.433" name="Leconfield Sar" region="yh" unitaryAuthArea="East Riding of Yorkshire" />
<Location elevation="3.0" id="3392" latitude="53.088" longitude="0.274" name="Wainfleet" region="em" unitaryAuthArea="Lincolnshire" />
<Location elevation="4.0" id="6" latitude="50.9561" longitude="0.9392" name="Lydd" region="se" unitaryAuthArea="Kent" />
<Location elevation="24.0" id="22" latitude="53.5797" longitude="-0.3472" name="Humberside Airport" region="yh" unitaryAuthArea="North Lincolnshire" />
<Location elevation="25.0" id="33" latitude="60.4322" longitude="-1.2992" name="Scatsta" region="os" unitaryAuthArea="Shetland Islands" />
<Location elevation="7.0" id="3006" latitude="60.447" longitude="-1.277" name="Sella Ness" region="os" unitaryAuthArea="Shetland Islands" />
<Location elevation="57.0" id="3008" latitude="59.527" longitude="-1.628" name="Fair Isle" region="os" unitaryAuthArea="Shetland Islands" />
<Location elevation="18.0" id="3037" latitude="57.257" longitude="-5.809" name="Skye/Lusa (Samos)" region="he" unitaryAuthArea="Highland" />
<Location elevation="773.0" id="3039" latitude="57.4175" longitude="-5.689" name="Bealach Na Ba" region="he" unitaryAuthArea="Highland" />
<Location elevation="1130.0" id="3041" latitude="56.822" longitude="-4.97" name="Aonach Mor" region="he" unitaryAuthArea="Highland" />
<Location elevation="99.0" id="99003" latitude="58.167" longitude="-4.733" name="Cassley" region="he" unitaryAuthArea="Highland" />
<Location elevation="48.0" id="99025" latitude="52.833" longitude="-1.25" name="Sutton Bonington" region="em" unitaryAuthArea="Nottinghamshire" />
<Location elevation="89.0" id="99057" latitude="52.017" longitude="-0.6" name="Woburn" region="ee" unitaryAuthArea="Central Bedfordshire" />
<Location elevation="140.0" id="99062" latitude="52.456" longitude="-1.9262" name="Winterbourne" region="wm" unitaryAuthArea="West Midlands Conurbation" />
<Location elevation="41.0" id="99078" latitude="52.4" longitude="-0.233" name="Monks Wood" region="ee" unitaryAuthArea="Cambridgeshire" />
<Location elevation="43.0" id="99086" latitude="54.667" longitude="-5.75" name="Helens Bay" region="ni" unitaryAuthArea="County Down" />
<Location elevation="20.0" id="3980" latitude="55.366" longitude="-7.333" name="Malin Head" />
<Location elevation="95.0" id="3405" latitude="52.789" longitude="-4.742" name="Aberdaron" region="wl" unitaryAuthArea="Gwynedd" />
<Location elevation="163.0" id="3409" latitude="52.917" longitude="-3.583" name="Bala" region="wl" unitaryAuthArea="Gwynedd" />
<Location elevation="72.0" id="3414" latitude="52.794" longitude="-2.663" name="Shawbury" region="wm" unitaryAuthArea="Shropshire" />
<Location elevation="138.0" id="3453" latitude="52.727" longitude="-0.654" name="Cottesmore" region="em" unitaryAuthArea="Rutland" />
<Location elevation="21.0" id="3482" latitude="52.651" longitude="0.569" name="Marham" region="ee" unitaryAuthArea="Norfolk" />
<Location elevation="14.0" id="3496" latitude="52.686" longitude="1.693" name="Hemsby" region="ee" unitaryAuthArea="Norfolk" />
<Location elevation="63.0" id="3503" latitude="52.344" longitude="-3.947" name="Trawsgoed" region="wl" unitaryAuthArea="Ceredigion" />
<Location elevation="307.0" id="3507" latitude="52.063" longitude="-3.614" name="Sennybridge" region="wl" unitaryAuthArea="Powys" />
<Location elevation="44.0" id="3604" latitude="51.708" longitude="-5.055" name="Milford Haven C.B." region="wl" unitaryAuthArea="Pembrokeshire" />
<Location elevation="59.0" id="3628" latitude="51.521" longitude="-2.576" name="Filton" region="sw" unitaryAuthArea="South Gloucestershire" />
<Location elevation="81.0" id="3649" latitude="51.758" longitude="-1.576" name="Brize Norton" region="se" unitaryAuthArea="Oxfordshire" />
<Location elevation="40.0" id="3672" latitude="51.548" longitude="-0.415" name="Northolt" region="se" unitaryAuthArea="Greater London" />
<Location elevation="348.0" id="3710" latitude="51.087" longitude="-3.608" name="Liscombe" nationalPark="Exmoor National Park" region="sw" unitaryAuthArea="Somerset" />
</Locations>
我想以这样的方式解析这个文件:
Location Elevation id latitude longitude name nationalpark region unitaryAuthArea
933.0 3072 56.879 -3.42 Cairnwell Cairngorms National Park ta Perth and Kinross
134.0 3088 56.852 -2.264 Inverbervie gr Aberdeenshire
我真的希望这种格式的数据能够以CSV格式导出。我是Python XML Parsing的新手,虽然之前已经引用过类似的问题,但之前从未真正做过XML.ETREE.ELEMENTTREE包。我迄今为止尝试的代码如下:
import xml.etree.ElementTree as ET
tree = ET.parse('sitelist.xml')
regions = tree.getroot()[0]
for region in regions.iter('name'):
print region.tag, region.attrib
任何人都可以帮助我解决这个问题,这将有助于我完成学术研究。
答案 0 :(得分:1)
ET
元素迭代他们的孩子。特殊的.iter()
函数迭代整个子树中给定名称的元素。它在这里是不必要的,只有在你寻找&#34;位置&#34;而不是&#34; name&#34;。在您的示例中,<Locations>
是树根,因此只需迭代根就可以获得所需的元素。
接下来,您必须提取所需的属性。您可以将它们放在列表中并使用get
来获取它们。最后,插入csv.writer
并完成它。
编辑:unicode字符帐户
import xml.etree.ElementTree as ET
import csv
import codecs
fields = ['id', 'latitude', 'longitude', 'name', 'nationalPark', 'region',
'unitaryAuthArea']
with open('output.csv', 'wb') as fp:
writer = csv.writer(fp)
writer.writerow(fields)
tree = ET.parse('test.xml')
# from your example Locations is the root and Location is the first level
for elem in tree.getroot():
writer.writerow([(elem.get(name) or '').encode('utf-8')
for name in fields])