Python3學習之路~5.9 xml處理模組
阿新 • • 發佈:2018-12-14
xml是實現不同語言或程式之間進行資料交換的協議,跟json差不多,但json使用起來更簡單,不過,以前在json還沒誕生的時候,大家只能選擇用xml,至今很多傳統公司如金融行業的很多系統的介面還主要是xml。
xml的格式如下,就是通過<>節點來區別資料結構的:
<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <yearView Code>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank updated="yes">69</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data>
xml協議在各個語言裡的都是支援的,在python中可以用以下模組操作xml
import xml.etree.ElementTree as ET tree = ET.parse('xmltest.xml') root = tree.getroot() print(root.tag) # data 根標籤 # 遍歷xml文件 for child in root: print(child.tag,child.attrib) for i in child: print(i.tag,i.text) # 只遍歷year 節點 for node in root.iter('year'): print(node.tag,node.text) # 修改 for node in root.iter('year'): new_year=int(node.text)+1 #加1 node.text=str(new_year) node.set("updated","yes") #加屬性 tree.write('xmltest.xml') # 刪除node for country in root.findall('country'): rank = int(country.find('rank').text) if rank>50: root.remove(country) tree.write('output.xml')xml-遍歷 修改 刪除
自己建立xml文件
import xml.etree.ElementTree as ET new_xml = ET.Element("infolist") #根節點 personinfo = ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"yes"}) name = ET.SubElement(personinfo,'name') name.text = 'Alice' age = ET.SubElement(personinfo,'age',attrib={"check":"no"}) age.text = '21' sex = ET.SubElement(personinfo,'sex') sex.text = 'F' personinfo2 = ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"yes"}) name = ET.SubElement(personinfo2,'name') name.text = 'Bob' age = ET.SubElement(personinfo2,'age',attrib={"check":"no"}) age.text = '30' sex = ET.SubElement(personinfo2,'sex') sex.text = 'M' et = ET.ElementTree(new_xml) # 生成文件物件 et.write("test.xml",encoding='UTF-8',xml_declaration=True) ET.dump(new_xml) # 列印生成的格式xml-建立