Python:如何读取包含所有属性的XML文件?

时间:2016-02-17 09:12:54

标签: python xml python-2.7 for-loop

我有一个给定天气数据的样本,并希望python读取相关的观察结果,以提供整洁可读的数据集。 XML文件的示例如下:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <UkExtremes extremeDate="2016-02-09" issuedAt="2016-02-09T23:03:30Z">
- <Regions>
- <Region id="sw" name="South West England">
- <Extremes>
  <Extreme locId="99159" locationName="Swanage" type="HMAXT" uom="degC">8.6</Extreme> 
  <Extreme locId="99094" locationName="Okehampton" type="LMAXT" uom="degC">3.5</Extreme> 
  <Extreme locId="03710" locationName="Liscombe" type="LMINT" uom="degC">0.6</Extreme> 
  <Extreme locId="99094" locationName="Okehampton" type="HRAIN" uom="mm">11.4</Extreme> 
  <Extreme locId="03740" locationName="Lyneham" type="HSUN" uom="hours">3.3</Extreme> 
  </Extremes>
  </Region>
- <Region id="uk" name="UK">
- <Extremes>
  <Extreme locId="03469" locationName="Holbeach" type="HMAXT" uom="degC">8.7</Extreme> 
  <Extreme locId="99129" locationName="Carterhouse" type="LMAXT" uom="degC">2.8</Extreme> 
  <Extreme locId="99016" locationName="Kinbrace" type="LMINT" uom="degC">-3.1</Extreme> 
  <Extreme locId="03212" locationName="Keswick" type="HRAIN" uom="mm">18.8</Extreme> 
  <Extreme locId="03377" locationName="Waddington" type="HSUN" uom="hours">6.4</Extreme> 
  </Extremes>
  </Region>
- <Region id="ni" name="Northern Ireland">
- <Extremes>
  <Extreme locId="03904" locationName="Castlederg" type="HMAXT" uom="degC">7.9</Extreme> 
  <Extreme locId="99175" locationName="Killylane" type="LMAXT" uom="degC">3.5</Extreme> 
  <Extreme locId="03923" locationName="Glenanne" type="LMINT" uom="degC">0.5</Extreme> 
  <Extreme locId="03904" locationName="Castlederg" type="HRAIN" uom="mm">7.8</Extreme> 
  <Extreme locId="03907" locationName="Magilligan" type="HSUN" uom="hours">0.9</Extreme> 
  </Extremes>
  </Region>
- <Region id="wl" name="Wales">
- <Extremes>
  <Extreme locId="99176" locationName="Usk No.2" type="HMAXT" uom="degC">8.5</Extreme> 
  <Extreme locId="03410" locationName="Lake Vyrnwy" type="LMAXT" uom="degC">3.3</Extreme> 
  <Extreme locId="03507" locationName="Sennybridge" type="LMINT" uom="degC">0.3</Extreme> 
  <Extreme locId="99194" locationName="Swyddffynnon" type="HRAIN" uom="mm">14.2</Extreme> 
  <Extreme locId="03716" locationName="St Athan" type="HSUN" uom="hours">2.8</Extreme> 
  </Extremes>
  </Region>
- <Region id="se" name="London & South East England">
- <Extremes>
  <Extreme locId="03770" locationName="St James Park" type="HMAXT" uom="degC">8.3</Extreme> 
  <Extreme locId="99154" locationName="Wych Cross" type="LMAXT" uom="degC">6.1</Extreme> 
  <Extreme locId="03658" locationName="Benson" type="LMINT" uom="degC">1.5</Extreme> 
  <Extreme locId="03882" locationName="Herstmonceux" type="HRAIN" uom="mm">10.0</Extreme> 
  <Extreme locId="03761" locationName="Odiham" type="HSUN" uom="hours">3.5</Extreme> 
  </Extremes>
  </Region>
- <Region id="st" name="Strathclyde">
- <Extremes>
  <Extreme locId="99006" locationName="Dunstaffnage" type="HMAXT" uom="degC">8.3</Extreme> 
  <Extreme locId="03155" locationName="Drumalbin" type="LMAXT" uom="degC">4.0</Extreme> 
  <Extreme locId="99024" locationName="Salsburgh" type="LMINT" uom="degC">1.8</Extreme> 
  <Extreme locId="03105" locationName="Port Ellen" type="HRAIN" uom="mm">13.8</Extreme> 
  <Extreme locId="03100" locationName="Tiree" type="HSUN" uom="hours">2.8</Extreme> 
  </Extremes>
  </Region>
- <Region id="ne" name="North East England">
- <Extremes>
  <Extreme locId="03275" locationName="Loftus" type="HMAXT" uom="degC">6.9</Extreme> 
  <Extreme locId="99186" locationName="Kielder Castle" type="LMAXT" uom="degC">3.8</Extreme> 
  <Extreme locId="99186" locationName="Kielder Castle" type="LMINT" uom="degC">2.4</Extreme> 
  <Extreme locId="99186" locationName="Kielder Castle" type="HRAIN" uom="mm">9.8</Extreme> 
  <Extreme locId="03240" locationName="Boulmer" type="HSUN" uom="hours">0.1</Extreme> 
  </Extremes>
  </Region>
- <Region id="dg" name="Dumfries,Galloway,Lothian & Borders">
- <Extremes>
  <Extreme locId="03132" locationName="West Freugh" type="HMAXT" uom="degC">6.7</Extreme> 
  <Extreme locId="99129" locationName="Carterhouse" type="LMAXT" uom="degC">2.8</Extreme> 
  <Extreme locId="99129" locationName="Carterhouse" type="LMINT" uom="degC">1.7</Extreme> 
  <Extreme locId="03162" locationName="Eskdalemuir" type="HRAIN" uom="mm">5.4</Extreme> 
  <Extreme locId="03166" locationName="Edinburgh Gogarbank" type="HSUN" uom="hours">0.8</Extreme> 
  </Extremes>
  </Region>
- <Region id="ta" name="Central Tayside & Fife">
- <Extremes>
  <Extreme locId="03144" locationName="Strathallan" type="HMAXT" uom="degC">8.0</Extreme> 
  <Extreme locId="99020" locationName="Tyndrum" type="LMAXT" uom="degC">7.4</Extreme> 
  <Extreme locId="03171" locationName="Leuchars" type="LMINT" uom="degC">1.7</Extreme> 
  <Extreme locId="99020" locationName="Tyndrum" type="HRAIN" uom="mm">13.6</Extreme> 
  <Extreme locId="03171" locationName="Leuchars" type="HSUN" uom="hours">5.4</Extreme> 
  </Extremes>
  </Region>
- <Region id="gr" name="Grampian">
- <Extremes>
  <Extreme locId="03066" locationName="Kinloss" type="HMAXT" uom="degC">7.0</Extreme> 
  <Extreme locId="99145" locationName="Balmoral" type="LMAXT" uom="degC">5.4</Extreme> 
  <Extreme locId="99010" locationName="Fyvie Castle" type="LMINT" uom="degC">-2.7</Extreme> 
  <Extreme locId="99010" locationName="Fyvie Castle" type="HRAIN" uom="mm">3.4</Extreme> 
  <Extreme locId="03091" locationName="Dyce" type="HSUN" uom="hours">4.8</Extreme> 
  </Extremes>
  </Region>
- <Region id="os" name="Orkney & Shetland">
- <Extremes>
  <Extreme locId="03017" locationName="Kirkwall" type="HMAXT" uom="degC">7.4</Extreme> 
  <Extreme locId="03005" locationName="Lerwick" type="LMAXT" uom="degC">6.5</Extreme> 
  <Extreme locId="03002" locationName="Baltasound" type="LMINT" uom="degC">1.4</Extreme> 
  <Extreme locId="03002" locationName="Baltasound" type="HRAIN" uom="mm">7.4</Extreme> 
  <Extreme locId="03005" locationName="Lerwick" type="HSUN" uom="hours">1.2</Extreme> 
  </Extremes>
  </Region>
- <Region id="em" name="East Midlands">
- <Extremes>
  <Extreme locId="03469" locationName="Holbeach" type="HMAXT" uom="degC">8.7</Extreme> 
  <Extreme locId="03354" locationName="Nottingham" type="LMAXT" uom="degC">6.6</Extreme> 
  <Extreme locId="03391" locationName="Coningsby" type="LMINT" uom="degC">0.0</Extreme> 
  <Extreme locId="99149" locationName="Coton-In-The-Elms" type="HRAIN" uom="mm">0.2</Extreme> 
  <Extreme locId="03377" locationName="Waddington" type="HSUN" uom="hours">6.4</Extreme> 
  </Extremes>
  </Region>
- <Region id="wm" name="West Midlands">
- <Extremes>
  <Extreme locId="03522" locationName="Hereford" type="HMAXT" uom="degC">8.2</Extreme> 
  <Extreme locId="99164" locationName="Pennerley" type="LMAXT" uom="degC">4.0</Extreme> 
  <Extreme locId="03520" locationName="Shobdon" type="LMINT" uom="degC">-0.2</Extreme> 
  <Extreme locId="03520" locationName="Shobdon" type="HRAIN" uom="mm">2.2</Extreme> 
  <Extreme locId="03414" locationName="Shawbury" type="HSUN" uom="hours">6.0</Extreme> 
  </Extremes>
  </Region>
- <Region id="nw" name="North West England">
- <Extremes>
  <Extreme locId="99169" locationName="Myerscough" type="HMAXT" uom="degC">7.7</Extreme> 
  <Extreme locId="03224" locationName="Spadeadam" type="LMAXT" uom="degC">3.3</Extreme> 
  <Extreme locId="99179" locationName="Nantwich" type="LMINT" uom="degC">1.2</Extreme> 
  <Extreme locId="03212" locationName="Keswick" type="HRAIN" uom="mm">18.8</Extreme> 
  <Extreme locId="03351" locationName="Rostherne" type="HSUN" uom="hours">4.0</Extreme> 
  </Extremes>
  </Region>
- <Region id="ee" name="East of England">
- <Extremes>
  <Extreme locId="03693" locationName="Shoeburyness" type="HMAXT" uom="degC">7.8</Extreme> 
  <Extreme locId="99136" locationName="Houghton Hall" type="LMAXT" uom="degC">6.2</Extreme> 
  <Extreme locId="03680" locationName="Rothamsted" type="LMINT" uom="degC">1.7</Extreme> 
  <Extreme locId="99078" locationName="Monks Wood" type="HRAIN" uom="mm">0.2</Extreme> 
  <Extreme locId="03462" locationName="Wittering" type="HSUN" uom="hours">4.9</Extreme> 
  </Extremes>
  </Region>
- <Region id="he" name="Highland & Eilean Siar">
- <Extremes>
  <Extreme locId="03075" locationName="Wick Airport" type="HMAXT" uom="degC">7.3</Extreme> 
  <Extreme locId="03031" locationName="Loch Glascarnoch" type="LMAXT" uom="degC">4.5</Extreme> 
  <Extreme locId="99016" locationName="Kinbrace" type="LMINT" uom="degC">-3.1</Extreme> 
  <Extreme locId="03044" locationName="Altnaharra" type="HRAIN" uom="mm">11.8</Extreme> 
  <Extreme locId="03063" locationName="Aviemore" type="HSUN" uom="hours">1.8</Extreme> 
  </Extremes>
  </Region>
- <Region id="yh" name="Yorkshire & Humber">
- <Extremes>
  <Extreme locId="03292" locationName="Bridlington" type="HMAXT" uom="degC">8.5</Extreme> 
  <Extreme locId="03346" locationName="Emley Moor" type="LMAXT" uom="degC">3.5</Extreme> 
  <Extreme locId="99177" locationName="Pateley Bridge Ravens Nest" type="LMINT" uom="degC">1.4</Extreme> 
  <Extreme locId="99177" locationName="Pateley Bridge Ravens Nest" type="HRAIN" uom="mm">12.8</Extreme> 
  <Extreme locId="99107" locationName="Sheffield" type="HSUN" uom="hours">6.0</Extreme> 
  </Extremes>
  </Region>
  </Regions>
  </UkExtremes>

我希望我在python中获取所有值,删除XML文件的所有标记。

我已通过以下命令导入文件:

import xml.etree.ElementTree as ET
tree = ET.parse('latest.xml')
root = tree.getroot()

然后我使用for循环来读取给定文件的属性。

for child in root:
    print child.attrib, child.tag

但是如何检索所有读取的数据。我不熟悉Python中的XML数据解析并使用python IDLE

1 个答案:

答案 0 :(得分:3)

XML的根标记为<UkExtremes>,循环显示子标记只会为您提供<Regions>标记。如果您的所有<Region>代码都带有<Extreme>值,则需要创建一个嵌套循环。

这将是类似的事情:

import xml.etree.ElementTree as ET

tree = ET.parse('weather.xml')
regions = tree.getroot()[0]
for region in regions:
   print "%s (%s)" % (region.attrib['name'], region.attrib['id'])
   print "-----------------------------"
   for extreme in region[0]:
       print extreme.attrib
       print extreme.text
   print "-----------------------------"
   print "-----------------------------"

由您决定如何查看字典输出。

作为旁注,您应该将xml中的所有&符号更改为&amp;nbsp;,因为xmltree将为您提供解析错误。