1. python字典转json报TypeError
如下字典转json出错 TypeError: Object of type ‘int64’ is not JSON serializable
para_result = [
[0],
[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],
[26]
]
经过检查是子列表里面元素值为numpy int类型,所以需要做下转化:
para_result = [[int(i) for i in para] for para in para_result]
2. 往json写汉字注意需要ensure_ascii=False
import json
with open('aa.json', 'w', encoding='utf-8') as f:
json.dump(result, f, ensure_ascii=False)