前言
最近在做数据分析,拿到一组json格式文件,但是文件却不是规范的json格式,需要将文件过滤一波,恢复规范的json格式。这里做个记录。
问题
json文件如下,其中"planning_status"
的value是个list,同时这个list占了5行,每行末尾都有换行符。
1 2 3 4 5 6 7 8
| { "planning_status": ["Now: 1390.04, Gap: 0.10, Delay: 0.20, Se: -0.3, Le: +0.06, Real ka al: 0.00036, 0.037, in_HDmap_mode"], "distance_to_ramp": 1291.777709961 }
|
解决
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| import json
fpath = "test.json" with open(fpath, 'r+', encoding='utf-8') as infile: lines = infile.readlines() pi_string = '' for line in lines: pi_string += line.strip()
j = json.loads(pi_string)
with open('test.json', 'w', encoding='utf-8') as outfile: json.dump(j, outfile, ensure_ascii=False)
def listToJson(lst): import json import numpy as np keys = [str(x) for x in np.arange(len(lst))] list_json = dict(zip(keys, lst)) str_json = json.dumps(list_json, indent=2, ensure_ascii=False) return str_json
|