序列化与反序列化
# 读取文件
f = open("input_compensate.txt", encoding='utf-8')
input_str = f.read()
f.close()
# 文件内容json转字典
input_dict = json.loads(input_str)
# 字典转json,ensure_ascii默认为true
json.dumps(expense_feature, ensure_ascii=False)
json格式化输出
json库方法
# import json
# print json.dumps({'a': 'Runoob', 'b': 7}, sort_keys=True, indent=4, separators=(',', ': '))
{
"a": "Runoob",
"b": 7
}
命令行格式化
# echo '{"name":"weick","age":"22","sex":"male"}' | python -m json.tool
{
"age": "22",
"name": "weick",
"sex": "male"
}
# python -m json.tool demo.json
{
"message": "success",
"data": {
"type": 5000,
"info": {
"text": "您要查询哪个城市的呢?",
"action": "询问城市"
},
"slu": {
"input": "天气",
"词法分析": {
"中文分词": [
"天气"
],
"词性标注": [
"普通名词"
],
"实体识别": [
"O"
]
},
"semantics": [
{
"score": 1.0000100135803223,
"domain": "天气",
"intent": "查天气",
"slot": []
}
]
}
}
}