1.提取data.json中的数据,将每条数据中的name、text、love和comment信息。并且保存到另外一个json文件中
import json with open('.\data.json','r',encoding='utf-8') as f: con = json.load(f) new = [] a = {} for x in con['data']: a = {'name': x['name'], 'text': x['text'], 'love': x['love'], 'comment': x['comment']} new.append(a) with open('.\json1.json', 'w', encoding='utf-8') as c: json.dump(new,c)
2. 统计data.json中comment数量超过1000的个数和它们的和
import json with open('.\data.json','r',encoding='utf-8') as f: con = json.load(f) y = 0 comment0 = 0 for x in con['data']: y += int(x['comment']) if int(x['comment']) > 1000: comment0 += 1 print('超过1000:',comment0) print('和: ',y)
将data.json文件中所有点赞数(love)对应的值超出1000的用k来表示,例如1000修改为1k, 1345修改为1.3k
import json with open('.\data.json','r',encoding='utf-8') as f: con = json.load(f) love1 = 0 for x in con['data']: if int(x['love']) >=1000: love1 = int(x['love']) / 1000 wee=str('%.1fk'%(love1)) x['love'] = wee print(wee) with open('.\data1.json','w',encoding='utf-8') as fd: json.dump(con,fd)