亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: xml相關(guān)問題 [打印本頁]

作者: 鑄劍長空    時(shí)間: 2014-03-04 15:58
標(biāo)題: xml相關(guān)問題
import xml.etree.ElementTree as ET
country_data_as_string="""<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
"""
tree = ET.parse('country_data.xml')
root = tree.getroot()
root = ET.fromstring(country_data_as_string)

      
tree=ET.ElementTree(root)

對于這個(gè)程序,我想修改其中的一個(gè)rank值或者是任意改變這三個(gè)rank值,該怎么弄?我試了下find()但是好像不行還是我寫的不對。求大神指點(diǎn)下
作者: timespace    時(shí)間: 2014-03-04 17:35
回復(fù) 1# 鑄劍長空
還沒搞定?文檔的“XPath support”,有幾個(gè)例子。下面是找第二個(gè)rank,兩種方法,找到后應(yīng)該會修改吧。
  1. >>> root.find('./country[2]/rank').text
  2. '4'
  3. >>> root.find("./country[@name='Singapore']/rank").text
  4. '4'
復(fù)制代碼

作者: 鑄劍長空    時(shí)間: 2014-03-05 17:34
跪謝。。當(dāng)時(shí)用的時(shí)候沒搞懂。。試了好多次不成。。多謝多謝。。回復(fù) 2# timespace


   
作者: 鑄劍長空    時(shí)間: 2014-03-08 13:53
你好,還有個(gè)問題想問下,我要用的xml的文件有很多層,比如


我想讀出Aera的值
import xml.etree.ElementTree as ET
with open('jiyi.xml') as fp:
    jiyi_as_string=fp.read()
tree = ET.parse('jiyi.xml')
root = tree.getroot()
root = ET.fromstring(jiyi_as_string)
x1=root.find('./Vsp_Geometry/Component_List[1]/Component/Section_List/Section/Area').text
print x1
這是我寫的程序
但是出現(xiàn)了
Traceback (most recent call last):
  File "F:\python練習(xí)\jiexi", line 4, in <module>
    tree = ET.parse('jiyi.xml')
  File "H:\Python27\lib\xml\etree\ElementTree.py", line 1182, in parse
    tree.parse(source, parser)
  File "H:\Python27\lib\xml\etree\ElementTree.py", line 656, in parse
    parser.feed(data)
  File "H:\Python27\lib\xml\etree\ElementTree.py", line 1642, in feed
    self._raiseerror(v)
  File "H:\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror
    raise err
ParseError: mismatched tag: line 243, column 39
這樣的錯(cuò)誤,不知道怎么修改。。

回復(fù) 2# timespace




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2