这篇教程python对json文件首尾添加特殊字符的处理实例写得很实用,希望能帮到您。 # coding:utf-8 import json
d=[] data = [] with open("b2.json", 'r', encoding='utf-8') as f: for idx, line in enumerate(f): d = json.loads(line) d = {'qid': d['qid'], 'category': d['category'], 'title': d['title'], 'answer': d['answer'], 'desc': d['desc']} if d["category"][0:2]=="烦恼": d["category"]= "1" elif d["category"][0:2]=="游戏": d["category"]= "2" elif d["category"][0:2]=="商业": d["category"]= "3" elif d["category"][0:2]=="娱乐": d["category"]= "4" elif d["category"][0:2]=="生活": d["category"]= "5" elif d["category"][0:2]=="教育": d["category"]= "6" elif d["category"][0:2]=="育儿": d["category"]= "7" elif d["category"][0:2]=="健康": d["category"]= "8" elif d["category"][0:2]=="文化": d["category"]= "9" elif d["category"][0:2]=="电脑": d["category"]= "10" elif d["category"][0:2]=="社会": d["category"]= "11" elif d["category"][0:2]=="电子": d["category"]= "12" elif d["category"][0:2]=="体育": d["category"]= "13" elif d["category"][0:2]=="汽车": d["category"]= "14" else: d["category"]= "15" #保存文件 file_name = './data/datab2' + '.json' with open(file_name,'a',encoding = 'utf-8') as f: #print(len(data_list)) data = json.dumps(d) a='[' b=']' ss=a+data+b f.write(ss)
Python实现不写硬盘上传文件 python转义替换应用实例 |