Python 处理错误的 json 文件
felicx 化神

前言

最近在做数据分析,拿到一组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()
# strip() 删除 string 字符串末尾的指定字符,
# 若参数为空,默认为空白符,包括空格、换行符、回车符、制表符

j = json.loads(pi_string) # 转为dict

# 重新写入
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) # json转为string
return str_json
 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
访客数 访问量