Day11—作业

  1. 提取data.json中的数据,将每条数据中的name、text、love和comment信息保存到另外一个json文件中。
import json

list1 = []
with open('./data.json', 'r', encoding='utf-8') as f:
    info = json.load(f)
    for item in info['data']:
        dict1 = {
            'name':item['name'],
            'text':item['text'],
            'love':item['love'],
            'comment':item['comment']
        }
        list1.append(dict1)
with open('./newdata.json', 'w', encoding='utf-8') as f:
    json.dump(list1, f)
  1. 统计data.json中comment数量超过1000的个数。
import json

count = 0
with open('./data.json', 'r', encoding='utf-8') as f:
    info = json.load(f)
    for item in info['data']:
        if int(item['comment']) > 1000:
            count += 1
print("统计data.json中comment数量超过1000的个数为:", count)

3.将data.json文件中所有点赞数(love)对应的值超出1000的用k来表示,例如1000修改为1k, 1345修改为1.3k。

import json

with open('./data.json', 'r', encoding='utf-8') as f:
    info = json.load(f)
    for item in info['data']:
        if int(item['love']) > 1000:
            item['love'] = str(int(item['love'])/1000)+'k'

with open('./data.json', 'w', encoding='utf-8') as f:
    json.dump(info, f)
  1. 写猜数字游戏,如果输入有误,提示重新输入,直达输入正确为止。比如:输入数字的时候没有按要求输入,提示重新输入。
import random

num = random.randint(1, 10)
count = 0
def guss_num():
    try:
        n = int(input('请输入数字:'))
        global count
        count += 1
        if n == num:
            print('恭喜你猜对了,共猜了%s次!'%(count))
            return
        elif n > num:
            print('猜大了')
            guss_num()
        else:
            print('猜小了')
            guss_num()
    except ValueError:
        print('输入错误,请重新输入!')
        guss_num()
guss_num()
  1. 写学生管理系统的添加学生功能(数据需要本地化),要求除了保存学生的基本信息以外还要保存学生的学号,但是学号需要自动生成,生成原则:
      添加第一个学生对应的学号是:py001
      第二次添加的学生的学号是:py002
    如果前面的学生因为各种原因被移除了,那后面添加学生的时候原则不变,就是比如上次已经添加到py012,那么前面不管有没有删除情况,再次添加学生的学号是py013。
list1 = []
name = input('请输入名字:')
age = input('请输入年龄:')

try:
    with open('./StudentInfo.json', 'r', encoding='utf8') as f:
        info = json.load(f)
except:
    with open('./StudentInfo.json', 'w', encoding='utf8') as f:
        dict1 = {'school':'千峰', 'student_data':[]}
        json.dump(dict1, f)
        with open('./StudentInfo.json', 'r', encoding='utf8') as f:
            info = json.load(f)

if not info['student_data']:
    school_num = 'py001'
else:
    for item in info['student_data']:
        num = item['school_num'][2:]
        list1.append(int(num))
    num_max = max(list1) + 1
    if num_max < 10:
        school_num = 'py' + '00' + str(num_max)
    elif num_max < 100:
        school_num = 'py' + '0' + str(num)
    else:
        school_num = 'py' + str(num)
dict1 = {'name': name, 'age': age, 'school_num': school_num}
info['student_data'].append(dict1)
with open('./StudentInfo.json', 'w', encoding='utf8') as f:
    json.dump(info, f)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 提取data.json中的数据,将每条数据中的name、text、love和comment信息。并且保存到另外一个...
    d4lx阅读 105评论 0 0
  • 最下方附上data.json文件内容 提取data.json中的数据,将每条数据中的name、text、love和...
    Deathfeeling阅读 201评论 0 1
  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,090评论 2 89
  • ——舍得 世事者无非成事与坏事,回想历史我们会不禁感叹,如果他当时那样做或许现在的后世便不是现在这样子。 我...
    贪婪的挖宝人阅读 1,434评论 2 0
  • 亲爱的爸爸妈妈 您们好!今天女儿抽空回了趟家,每一次回家都有不一样的感受。以前是大学时期周末回家,后来是婚后怀孕了...
    我是小小彦阅读 233评论 0 0