加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml模块

发布时间:2020-12-16 23:44:41 所属栏目:百科 来源:网络整理
导读:shelve:模块 import shelve shelve.open(‘db.shl‘) f[‘su1‘]=[‘name‘:‘bn‘,‘age‘:34] #把su1当做key,后面的当做值,然后放进db.sh1文件里面 f[‘su2‘]=[‘name‘:‘bn2‘,‘age‘45] print(f[‘su1‘][‘name‘]) f.close() xml:模块 from xml
shelve:模块 import shelve shelve.open(‘db.shl‘) f[‘su1‘]=[‘name‘:‘bn‘,‘age‘:34] #把su1当做key,后面的当做值,然后放进db.sh1文件里面 f[‘su2‘]=[‘name‘:‘bn2‘,‘age‘45] print(f[‘su1‘][‘name‘]) f.close() xml:模块 from xml.etree import ElementTree tree=ElementTree.parse(‘a.xml‘) #xml文件 root=tree.getroot() #拿到根 print(root.tag) #标签的名字 print(root.attrib) #看属性 print(root.text) #文本 #三种查找方式 #1.从子节点中找 print(root.find()) root.findall() #2.从整树型结构中查找 root.iter(‘rank‘) #全篇文件找 print(list(root.iter())) #遍历文档树 for country in root: print(country.attrib[‘name‘]) for item in country: print(item.tag,item.attrib,item.text) #找文件中的时间 for year in root.iter(‘year‘): print(year.tag,year.attrib,year.text) #修改 for year in root.iter(‘year‘): year.set(‘update‘,"yes") year.text=str((year.text)+1) tree.write(‘a.xml‘) #添加节点 for country in root: obj=ElementTree.Element() #<egon name="egon" age="18">egon is good</egon> obj.attrib={‘name‘:‘egon‘,‘age‘:‘18‘} obj.text=‘egon is good‘ country.append(obj) tree.write(‘a.xml‘)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读