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)
你读取不了是因为你的字符串里有个中文的冒号