python - txt转为json是出现问题

查看:136
本文介绍了python - txt转为json是出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

txt文档的样式:

{"success": true, "scores": [{"analysis_details": {"location_dataset": "DEMOG_SACQ", "location_variable": "STUDY_SITE", "id": "5976e26cd5436459063b4ccd", "name": "DQA_20170725"}, "country": "CANADA", "rank": 1.0, "location": "1038", "size": 4, "result_id": 2231, "pvalue": 0.0199099, "analysis_id": "5976e26cd5436459063b4ccd"}, {"analysis_details":{"location_dataset": "DEMOG_SACQ", "location_variable": "STUDY_SITE", "id": "5976e26cd5436459063b4ccd", "name": "DQA_20170725"}, "country": "UNITED STATES", "rank": 2.0, "location": "1077", "size": 4, "result_id": 2240, "pvalue": 0.0441441, "analysis_id": "5976e26cd5436459063b4ccd"}]}

实际上就是字典结构的数据,我想要把它转化为json格式,然后方便保存和转成其他形式输出。

目前的code,然而不起作用。请大家指教一下:

f = open('filename1.txt','w')
jsonfile = json.loads(f)
f.close()

data = []
for d in jsonfile['scores']:

dic = dict(success=True)
dic.update(dict2til(d, {}, 'scores'))
data.append(dic)

解决方案

你读取不了是因为你的字符串里有个中文的冒号

import json

with open('a.txt') as f:
    d = json.load(f)
    print(d)

这篇关于python - txt转为json是出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆