Fork me on GitHub
# import shelve
# f=shelve.open('db.shl')
# # f['stu1']={'name':'alex1','age':28}
# # f['stu2']={'name':'alex2','age':18}
# print(f['stu1']) # {'name': 'alex1', 'age': 28}
# print(f['stu1']['name']) # alex1
# f.close()

'''
<?xml version="1.0"?>
<data v="1.0">
xxxxx
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>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>
'''

# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
#
# root=tree.getroot()
# print(root.tag)
# print(root.attrib)
# print(root.text)
'''
data
{'v': '1.0'}

xxxxx
'''
#三种查找方式
#从子节点找 找一个
# root.find()
#
# 从树形结构中查找
# root.iter()

# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# print(root.find('country'))
# print(root.findall('country'))
'''
<Element 'country' at 0x00000255D4EEEE58>
[<Element 'country' at 0x00000255D4EEEE58>, <Element 'country' at 0x00000255D4EF3098>, <Element 'country' at 0x00000255D4EF3278>]
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# print(list(root.iter('rank')))
'''
[<Element 'rank' at 0x0000025ED931EE08>, <Element 'rank' at 0x0000025ED9323048>, <Element 'rank' at 0x0000025ED9323278>]
'''
# print(root.findall('country'))
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root.findall('country'):
# print(country)
'''
<Element 'country' at 0x000002647FD7DE58>
<Element 'country' at 0x000002647FD83098>
<Element 'country' at 0x000002647FD83278>
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root.findall('country'):
# rank=country.find('rank')
# print(rank)
'''
<Element 'rank' at 0x0000015FEED8DE58>
<Element 'rank' at 0x0000015FEED93098>
<Element 'rank' at 0x0000015FEED932C8>
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root.findall('country'):
# rank=country.find('rank')
# print(rank.tag,rank.attrib,rank.text)
'''
rank {'updated': 'yes'} 2
rank {'updated': 'yes'} 5
rank {'updated': 'yes'} 69
'''
# 遍历文档树
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for item in root:
# print(item)
'''
<Element 'country' at 0x000001EA4A62CE58>
<Element 'country' at 0x000001EA4A633098>
<Element 'country' at 0x000001EA4A633278>
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root:
# print(country.tag)
'''
country
country
country
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root:
# print(country.attrib)
# print(country.attrib['name'])
# print('==>',country.attrib['name'])
'''
{'name': 'Liechtenstein'}
Liechtenstein
==> Liechtenstein
{'name': 'Singapore'}
Singapore
==> Singapore
{'name': 'Panama'}
Panama
==> Panama
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root:
# print('==>', country.attrib['name'])
# for item in country:
# print(item.tag,item.attrib,item.text)
'''
==> Liechtenstein
rank {'updated': 'yes'} 2
year {} 2008
gdppc {} 141100
neighbor {'name': 'Austria', 'direction': 'E'} None
neighbor {'name': 'Switzerland', 'direction': 'W'} None
==> Singapore
rank {'updated': 'yes'} 5
year {} 2011
gdppc {} 59900
neighbor {'name': 'Malaysia', 'direction': 'N'} None
==> Panama
rank {'updated': 'yes'} 69
year {} 2011
gdppc {} 13600
neighbor {'name': 'Costa Rica', 'direction': 'W'} None
neighbor {'name': 'Colombia', 'direction': 'E'} None
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for year in root.iter('year'):
# print(year)
'''
<Element 'year' at 0x0000028C4CB9DF48>
<Element 'year' at 0x0000028C4CBA3188>
<Element 'year' at 0x0000028C4CBA33B8>
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for year in root.iter('year'):
# print(year.tag,year.attrib,year.text)
'''
year {} 2008
year {} 2011
year {} 2011
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for year in root.iter('year'):
# year.set('updated','yes')
# year.text=str(int(year.text)+1)
# tree.write('a.xml')

# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root:
# obj=ElementTree.Element('egon')
# obj.attrib={'name':'egon','age':'18'}
# obj.text='egon is good'
# country.append(obj)
# tree.write('a.xml')

# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for rank in root.iter('rank'):
# if int(rank.text) == 5:
# obj=ElementTree.Element('egon')
# obj.attrib={'name':'egon','age':'18'}
# obj.text='egon is good'
# rank.append(obj)
# tree.write('a.xml')
posted on 2019-12-16 20:03  OBOS  阅读(127)  评论(0编辑  收藏  举报