作业15

一、将你前后左右的信息编写为xml文档,并在python中解析该文件,进行展示

# b.xml文件内容
<?xml version="1.0"?>
<data>
    <ahead name = "zyy" sex = "male" age="20" weight ="180" height="170">zyy</ahead>
    <back name = "zwj" sex = "male" age="23" weight="140" height="173">ryc</back>
    <left name = "zs" sex = "male" age="15" weight="100" height="177">zs</left>
    <right name = "ryc" sex = "male" age="19" weight="130" height="190">zwj</right>
</data>

import xml.etree.ElementTree as ET
tree = ET.parse('b.xml')    
root = tree.getroot() 
res = root.iter('right')
for item in res:
    print(item.attrib)

  

二、使用json格式存储一些歌曲信息包括(流派、歌曲名、作者、年份),实现输入流派展示对应的歌曲

import json,time
category = {'sentimental' : {'song_name': '你还要我怎样', 'author': '薛之谦', 'time': 2013},
'happy' : {'song_name': '恋爱ing', 'author': '五月天', 'time': 2005},
'motivation' : {'song_name': '倔强', 'author': '五月天', 'time': 2001},
'absolute_music' : {'song_name': '雨的印记', 'author': '佚名', 'time': 2000}}

with open('a.json', 'wt', encoding='utf-8') as f: json.dump(category, f) ================================================== 执行代码 tag = True while tag: cate_gory = input('请输入您喜欢听的风格>>: ').strip() with open('a.json', 'rt', encoding='utf-8') as f: data = json.load(f) for i in data: if i == cate_gory: print('正在搜索...') time.sleep(2) print(data[cate_gory]) tag = False break else: print('输入风格不存在,请重新输入')

三、练习:做一个登陆程序,首先查看配置文件,是否有包含用户名和密码,如果有则直接登陆,如果没有就要求输入用户密码。登陆成功后询问是否保存密码,如果是写入配置文件。

 

posted @ 2018-10-17 23:23  BlackLinks  阅读(83)  评论(0编辑  收藏  举报