day21作业

1.将你前后左右的同学的信息编写为xml文档
并在python中解析该文件 进行展示

import xml.etree.ElementTree as ET
root = ET.parse('stu.xml')
tree = root.getroot()
for item in tree.iter('stu'):
    print(item.find('name').text)
    print(item.find('age').text)

#scg
#22
#lhy
#24
<?xml version="1.0" encoding="utf-8" ?>
<info>
    <stu>
        <name>scg</name>
        <age>22</age>
    </stu>
    <stu>
        <name>lhy</name>
        <age>24</age>
    </stu>
</info>

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

import json
with open(r'music.json','r',encoding='utf-8') as f:
    res = json.load(f)
liupai = input('请输入流派:
').strip()
for item in res:
    if liupai == item['liupai']:
        print(item)
[
  {"liupai":"chinese",
  "name":"aaa",
  "author":"bbb",
  "year":1993},
  {"liupai":"korea",
  "name":"aaa1",
  "author":"bbb1",
  "year":1995},
  {"liupai":"chinese1",
  "name":"aaa2",
  "author":"bbb2",
  "year":1994}
]

3.练习:
做一个登录 首先查看配置文件 是否又包含 用户名和密码 如果由直接登录 如果没有就进行输入用户名密码登录
登录成功后 询问是否要保存密码 如果是 写入配置文件

def login_auth():
    import configparser
    config = configparser.ConfigParser()
    config.read('user.cfg',encoding='utf-8')
    if config.has_option('user','name'):
        print('登陆成功')
    else:
        name = input('请输入用户名登录:
').strip()
        pwd = input('请输入用户密码:
').strip()
        if name == 'abc' and pwd == '123':
            config.set('user','name',name)
            config.set('user','pwd',pwd)
            config.write(open('user.cfg','w',encoding='utf-8'))
            print('登陆成功')
[user]
name = abc
pwd = 123
原文地址:https://www.cnblogs.com/msj513/p/9806699.html