1.提取data.json中的数据,将每条数据中的name、text、love和comment信息。并且保存到另外一个json文件中
import json
content_name = []
content_text = []
content_love = []
content_comment = []
content2 = {}
with open('./data.json','r',encoding = 'utf-8') as f1:
content = json.load(f1)
for item in content['data'] :
for item1 in item:
if item1 == 'name':
content_name.append(item[item1])
if item1 == 'text':
content_text.append(item[item1])
if item1 == 'love':
content_love.append(item[item1])
if item1 == 'comment':
content_comment.append(item[item1])
content2 = {'name':content_name,'love':content_love,'text':content_text,'comment':content_comment}
with open('./new_data.json','w',encoding = 'utf-8') as f2:
json.dump(content2,f2)
2.统计data.json中comment数量超过1000的个数
import json
count = 0
with open('./data.json','r',encoding = 'utf-8') as f1:
content = json.load(f1)
for item in content['data']:
for item1 in item:
if item1 == 'comment' and float(item['comment']) >= 1000:
count += 1
print(count)
3.将data.json文件中所有点赞数(love)对应的值超出1000的用k来表示,例如1000修改为1k, 1345修改为1.3k
import json
with open('./data.json','r',encoding = 'utf-8') as f1:
content = json.load(f1)
for index in range(len(content['data'])):
for item in content['data'][index]:
if item == 'love' and float(content['data'][index][item]) >= 1000:
content['data'][index][item]=str(float(content['data'][index][item])/1000)+'k'
with open('./data.json','w',encoding = 'utf-8') as f2:
json.dump(content,f2)
4.写猜数字游戏,如果输入有误,提示重新输入,直达输入正确为止。比如:输入数字的时候没有按要求输入,提示重新输入
import random
num = random.randint(0,10)
while True:
try:
n = int(input('请输入数字:'))
if n == num:
print('输入正确')
break
elif n>=num:
print('大了,输小点,请重新输入')
else:
print('小了,输大点,请重新输入')
except ValueError:
print('语法错误,请重新输入')
5.写学生管理系统的添加学生功能(数据需要本地化),要求除了保存学生的基本信息以外还要保存学生的学号,但是学号需要自动生成,生成原则:
添加第一个学生对应的学号是:py001 第二次添加的学生的学号是:py002 ... 如果前面的学生因为各种原因被移除了,那后面添加学生的时候原则不变,就是比如上次已经添加到py012,那么前面不管有没有删除情况,再次添加学生的学号是py013
import json
student_info =[]
while True:
print('1.添加学生信息')
print('2.删除学生信息')
print('3.退出')
num = int(input('请选择:'))
if num == 1:
while True:
try:
with open('./学生管理系统.json', 'r', encoding='utf-8') as f:
student_info = json.load(f)
count = len(student_info)
student_name = input('请输入学生名字:')
student_age = input('请输入学生年龄:')
student_tel = input('请输入学生电话:')
count += 1
str1 = 'py'+ str(count).rjust(3,'0')
stu = {'name:': student_name,'age:':student_age,'tel':student_tel,'stu_id:':str1}
student_info.append(stu)
with open('./学生管理系统.json', 'w', encoding='utf-8') as f:
json.dump(student_info, f,ensure_ascii=False,indent=2)
except:
with open('./学生管理系统.json', 'w', encoding='utf-8') as f:
student_name = input('请输入学生名字:')
student_age = input('请输入学生年龄:')
student_tel = input('请输入学生电话:')
count =1
str1 = 'py' + str(count).rjust(3, '0')
stu = {'name:': student_name, 'age:': student_age, 'tel': student_tel, 'stu_id:': str1}
student_info.append(stu)
json.dump(student_info,f,ensure_ascii=False,indent=2)
print('1.继续添加')
print('2.添加结束,返回')
num1 = int(input('请选择:'))
if num1 == 2:
break
elif num ==2:
try:
with open('./学生管理系统.json', 'r', encoding='utf-8') as f:
del_name = input('输入要删除的学生姓名:')
for item in student_info:
if item['name'] ==del_name:
student_info.remove(item)
else:
print('请输入正确的学生姓名')
with open('./学生管理系统.json', 'w', encoding='utf-8') as f:
json.dump(student_info, f,ensure_ascii=False,indent=2)
except:
print('没有任何学生信息')
else:
break