- 题目:写函数,让用户输入用户名和密码,将密码转化成密文,然后构建一个字典,字典的键为用户名,值为其对应的密码,将这个字典以json字符串的形式写入文件中。
import hashlib
import json
def register():
user_dict = {}
username = input('请输入您的用户名:').strip()
password = input('请输入您的密码:').strip()
pwd = hashlib.md5(username[:3].encode('utf-8'))
pwd.update(password.encode('utf-8'))
user_dict[username] = pwd.hexdigest()
with open('pwd.json', encoding='utf-8', mode='a') as f:
f.write(json.dumps(user_dict)+'\n')
register()
with open('pwd.json', encoding='utf-8', mode='r') as ff:
for line in ff:
user = json.loads(line)
print(user, type(user))
|