python之xml和shelve模块


一,xml 模块

import xml.etree.ElementTree as ET

tree = ET.parse("a.xml")
root = tree.getroot()
# print(root.tag)
# print(root.attrib)
# print(root.text)

# res=root.iter('year') # 从整个树形结构中寻找
# print(list(res))
# for item in res:
#     print(item.tag,item.attrib,item.text)

# res=root.find('year') # 从当前节点的儿子中查找
# print(res)

# res=root.find('country') # 从当前节点的儿子中查找,找成功 一个就结束
# print(res.attrib)

# res=root.find('country').find('year')
# print(res.text)


# res=root.findall('country') # 从当前节点的儿子中查找,找到所有
# print(res)

# 遍历
# for country in root:
#     print('==============>',country.tag,country.attrib)
#     for item in country:
#         print(item.tag,item.attrib,item.text)


# 改
# for year in root.iter('year'):
    # print(year.text)
    # year.text=str(int(year.text) + 1)
    # print(year.text)
    # year.attrib={'updated':'yes'}
    # year.attrib={}
    # year.attrib.pop('updated')

# tree.write('b.xml')
# tree.write('a.xml')

# 删
# for country in root:
#     rank=country.find('rank')
#     v=int(rank.text)
#     if v > 50:
#         # print(rank.text)
#         root.remove(country)
#
# tree.write('a.xml')

# 增
# for country in root:
    # tag=ET.Element('egon')
    # tag.attrib={'age':"1",'sex':'male'}
    # tag.text='老男孩的最帅的老师'
    # country.append(tag)


# tree.write('a.xml')

for country in root:
    tag=country.find('egon')
    print(tag.text)

  

二,shelve 模块

import shelve

# userinfo={
#     'egon':{'age':18,'sex':'male'},
#     'alex':{'age':38,'sex':'female'},
# }

f=shelve.open(r'sheve.txt',writeback=True)
# f['egon']={'age':18,'sex':'male'}
# f['alex']={'age':38,'sex':'female'}
# f['egon']['age']=19

print(f['egon']['age'])
f.close()

  

优质内容筛选与推荐>>
1、Unique Binary Search Trees
2、Cordova学习
3、js fix小数点 和int的区别
4、my21_myloader -o参数
5、Selenium IDE 基础使用教程


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn