<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
關於XML的介紹
<data> 與 </data> 是一對標籤的開始與結束
<property … /> 也是一個正確的標籤,以 /> 結尾,是在標籤沒有巢狀內容時的簡寫形式
name=“cat”,name是<data>標籤的一個屬性,cat是name屬性的值
description here …是<data>標籤的內容,這裡是一段文字。當然也可以是xml的巢狀
<data name="cat" num="10"> description here ... </data> <property value="node" /> <country name="china"> <province name="beijing"> <school name="the sunshine school" /> </province> </country>
準備一個demo.xml檔案
<data> <teacher name="Albert"> <birthday>1980</birthday> <gender>male</gender> <subject>Math</subject> </teacher> <student name="Becky"> <birthday>2000</birthday> <gender>female</gender> <hobbies> <hobby>skating</hobby> <hobby>rocks</hobby> </hobbies> <exam absence="no"> <math>90</math> <english>90</english> <music>95</music> </exam> </student> <student name="Cindy"> <birthday>2001</birthday> <gender>female</gender> <hobbies> <hobby>reading</hobby> <hobby>guitar</hobby> </hobbies> <exam absence="yes"> </exam> </student> <student name="Duke"> <birthday>2000</birthday> <gender>male</gender> <hobbies> <hobby>football</hobby> <hobby>surfing</hobby> </hobbies> <exam absence="no"> <math>100</math> <english>80</english> <music>92</music> </exam> </student> </data>
讀取xml檔案內容
# Read the .xml file tree = ET.parse("demo.xml") root = tree.getroot() print(root)
結果
<Element 'data' at 0x102d80cf8>
for … in … 可以遍歷當前元素的所有直接子節點
for n in root: # items() returns all <key, value> pairs of the tag print(n, n.tag , n.attrib, n.items())
結果
(<Element 'teacher' at 0x1048b9e48>, 'teacher', {'name': 'Albert'}, [('name', 'Albert')])
(<Element 'student' at 0x1048bf0f0>, 'student', {'name': 'Becky'}, [('name', 'Becky')])
(<Element 'student' at 0x1048bf3c8>, 'student', {'name': 'Cindy'}, [('name', 'Cindy')])
(<Element 'student' at 0x1048bf5f8>, 'student', {'name': 'Duke'}, [('name', 'Duke')])
想要迭代遍歷當前元素的所有子節點(包括子孫節點)
for n in root.iter(): print(n, n.tag)
結果
(<Element 'data' at 0x1052f0cf8>, 'data')
(<Element 'teacher' at 0x1052f0e48>, 'teacher')
(<Element 'birthday' at 0x1052f0d30>, 'birthday')
(<Element 'gender' at 0x1052f6080>, 'gender')
(<Element 'subject' at 0x1052f60b8>, 'subject')
(<Element 'student' at 0x1052f60f0>, 'student')
(<Element 'birthday' at 0x1052f6048>, 'birthday')
(<Element 'gender' at 0x1052f6128>, 'gender')
(<Element 'hobbies' at 0x1052f6198>, 'hobbies')
(<Element 'hobby' at 0x1052f6208>, 'hobby')
(<Element 'hobby' at 0x1052f6240>, 'hobby')
(<Element 'exam' at 0x1052f62b0>, 'exam')
(<Element 'math' at 0x1052f6320>, 'math')
(<Element 'english' at 0x1052f6390>, 'english')
(<Element 'music' at 0x1052f6400>, 'music')
(<Element 'student' at 0x1052f63c8>, 'student')
(<Element 'birthday' at 0x1052f6438>, 'birthday')
(<Element 'gender' at 0x1052f6470>, 'gender')
(<Element 'hobbies' at 0x1052f64a8>, 'hobbies')
(<Element 'hobby' at 0x1052f6518>, 'hobby')
(<Element 'hobby' at 0x1052f6588>, 'hobby')
(<Element 'exam' at 0x1052f65c0>, 'exam')
(<Element 'student' at 0x1052f65f8>, 'student')
(<Element 'birthday' at 0x1052f6630>, 'birthday')
(<Element 'gender' at 0x1052f6668>, 'gender')
(<Element 'hobbies' at 0x1052f66a0>, 'hobbies')
(<Element 'hobby' at 0x1052f6710>, 'hobby')
(<Element 'hobby' at 0x1052f6780>, 'hobby')
(<Element 'exam' at 0x1052f67b8>, 'exam')
(<Element 'math' at 0x1052f6828>, 'math')
(<Element 'english' at 0x1052f6898>, 'english')
(<Element 'music' at 0x1052f6908>, 'music')
想要選擇性地迭代直接子節點
for n in root.iter('teacher'): print(n, n.tag)
(<Element 'teacher' at 0x100f29e48>, 'teacher')
find與findall查詢xml元素
# find the first element print(root.find('student')) # find all elements print(root.findall('student'))
<Element 'student' at 0x1034300f0> [<Element 'student' at 0x1034300f0>, <Element 'student' at 0x1034303c8>, <Element 'student' at 0x1034305f8>]
demo
for n in root: if n.tag == 'student' and n.get('name') == 'Becky': exam_node = n.find('exam') for subject in exam_node: print(subject.tag + " " + subject.text)
結果
math 90
english 90
music 95
p = ET.Element(tag_name)
demo
for n in root: if n.tag == 'student' and n.get('name') == 'Cindy': exam_node = n.find('exam') exam_node.set("absence", "no") for subject in ['math', 'music']: p = ET.Element(subject) p.text = '90' exam_node.append(p) if os.path.exists('new.xml'): os.remove('new.xml') tree.write('new.xml', encoding='utf-8', xml_declaration=True)
結果
<student name="Cindy">
<birthday>2001</birthday>
<gender>female</gender>
<hobbies>
<hobby>reading</hobby>
<hobby>guitar</hobby>
</hobbies>
<exam absence="no">
<math>90</math><music>90</music></exam>
</student>
demo
for n in root: if n.tag == 'student' and n.get('name') == 'Cindy': exam_node = n.find('exam') exam_node.set("absence", "no") exam_node.set("date", "2022-11-11") for subject in ['math', 'music']: p = ET.Element(subject) p.text = '90' exam_node.append(p) hobbies_node = n.find('hobbies').findall("hobby") hobbies_node[0].text = 'piano' p = ET.Element("hobby") p.set("old_hobby", 'yes') p.text = 'reading' n.find('hobbies').remove(hobbies_node[1]) n.find('hobbies').append(p)
結果
<student name="Cindy">
<birthday>2001</birthday>
<gender>female</gender>
<hobbies>
<hobby>piano</hobby>
<hobby old_hobby="yes">reading</hobby></hobbies>
<exam absence="no" date="2022-11-11">
<math>90</math><music>90</music></exam>
</student>
到此這篇關於Python對XML檔案實現增刪改查操作的文章就介紹到這了,更多相關Python XML增刪改查內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45