作业十一。

 1. 将你前后左右的同学信息编写成xml文件,并在python中解析并展示

  创建一个xml文件:

      student.xml

      文件里的内容:

        

<students>
<stu name="任振辉" age="16" height="180CM" weight="85KG"/>
<stu name="袁冲" age="23" height="173CM" weight="75KG"/>
<stu name="孔令飞" age="23" height="168CM" weight="60KG"/>
</students>


  创建一个py文件:
      student.py
      文件里的内容
import xml.etree.ElementTree as ET
stuinfo = ET.parse('student.xml')
stus = stuinfo.getroot()
for item in stus.iter('stu'):
user = item.attrib
print(user)



2. 用json格式存储一些歌曲信息
   包括
   流派
   歌曲名称
   作者
   年代
   实现 输入流派展示对应的歌曲

      先创建一个json文件:

      文件的内容是:

    

{
"歌曲库":{
"摇滚":[
{
"歌曲名":"突然好想你",
"作者 ":"五月天",
"年代 ":"90后"
}
]
}
}

再创建一个py.文件
    文件的内容是:
import json
with open('songs.json','rt',encoding='utf-8') as f:
res = json.load(f)
choice = input('请输入你想要查询的歌曲类型>>:')
for s in res['歌曲库']['%s'%choice]:
for key in s:
print('%s:%2s'%(key,s[key]))



  
原文地址:https://www.cnblogs.com/lvyipin1/p/9808033.html