我正在尝试从一大批文件中提取一些数据,并将它们转换为特定的(JSON)格式,以便使用Django Fixtures导入到数据库中
我已经做到了这一点:
'{ {\n "pk":2,\n“模型”:trials.conditions,\n“字段”:{\n "trial_id":NCT00109798,\n“关键字”:大脑和中枢神经系统肿瘤,\n }{\n "pk":3,\n“模型”:trials.conditions,\n“字段”:{\n "trial_id":NCT00109798,\n“关键字”:淋巴瘤,\n }{\n "pk":2,\n“模型”:trials.criteria,\n“字段”:{\n "trial_id":NCT00109798,\n“性别”:两者,\n "minimum_age":18年,\n "maximum_age":不适用,\n "healthy_volunteers":否,\n "textblock":,\n }\n\t\t“主键”:2,\n\t\t“模型”:trials.keyword,\n\t\t“字段”:{\n\t\t"trial_id":NCT00109798,\n\t\t“关键字”:原发性中枢神经系统非霍奇金淋巴瘤,\n\t\t}\n\t\t
稍后的...many行.....
在完成研究治疗后,患者每3个月随访1年,\n每4个月随访1年,然后每6个月随访3年。\n \n预计应计:本研究将累计6-25名患者。\n,\n "overall_status":招募,\n“阶段”:第二阶段,\n“登记”:25,\n "study_type":干预性,\n "condition":2,3,\n "criteria":1,\n "overall_contact":testdata,\n "location":4,\n "lastchanged_date":2010年3月31日\n "firstreceived_date":2005年5月3日,\n "keyword":2,3,\n "condition_mesh":,\n }\n \n {\n "pk":测试数据,\n“模型”:trials.contact,\n“字段”:{\n "trial_id":NCT00109798,\n "last_name":Pamela Z. New,MD,\n“电话”:,\n“电子邮件”:,\n }}‘
输出实际上需要如下所示:
{
"pk": trial_id,
"model": trials.trial,
"fields": {
"trial_id": trial_id,
"brief_title": brief_title,
"official_title": official_title,
"brief_summary": brief_summary,
"detailed_Description": detailed_description,
"overall_status": overall_status,
"phase": phase,
"enrollment": enrollment,
"study_type": study_type,
"condition": _______________,
"elligibility": elligibility,
"criteria": ______________,
"overall_contact": _______________,
"location": ___________,
"lastchanged_date": lastchanged_date,
"firstreceived_date": firstreceived_date,
"keyword": __________,
"condition_mesh": condition_mesh,
}
"pk": null,
"model": trials.locations,
"fields": {
"trials_id": trials_id,
"facility": facility,
"city": city,
"state": state,
"zip": zip,
"country": country,
}任何建议都将不胜感激。
发布于 2011-11-10 01:19:48
json.dumps缩进参数的替代方法:
Python在http://docs.python.org/library/pprint.html上有一台漂亮的打印机。它使用起来非常简单,但只打印python对象(你不能给它一个json字符串并期望格式化的输出)
例如:
pydict = {"name":"Chateau des Tours Brouilly","code":"chateau-des-tours-brouilly-2009-1","region":"France > Burgundy > Beaujolais > Brouilly","winery":"Chateau Des Tours","winery_id":"chateau-des-tours","varietal":"Gamay","price":"14.98","vintage":"2009","type":"Red Wine","link":"http://www.snooth.com/wine/chateau-des-tours-brouilly-2009-1/","tags":"colorful, mauve, intense, purple, floral, violet, lively, rich, raspberry, berry","image":"http://ei.isnooth.com/wine/b/7/8/wine_6316762_search.jpeg","snoothrank":3,"available":1,"num_merchants":10,"num_reviews":1}
from pprint import pprint
pprint(pydict)输出为
{'available': 1,
'code': 'chateau-des-tours-brouilly-2009-1',
'image': 'http://ei.isnooth.com/wine/b/7/8/wine_6316762_search.jpeg',
'link': 'http://www.snooth.com/wine/chateau-des-tours-brouilly-2009-1/',
'name': 'Chateau des Tours Brouilly',
'num_merchants': 10,
'num_reviews': 1,
'price': '14.98',
'region': 'France > Burgundy > Beaujolais > Brouilly',
'snoothrank': 3,
'tags': 'colorful, mauve, intense, purple, floral, violet, lively, rich, raspberry, berry',
'type': 'Red Wine',
'varietal': 'Gamay',
'vintage': '2009',
'winery': 'Chateau Des Tours',
'winery_id': 'chateau-des-tours'}发布于 2011-11-06 10:32:48
在json模块中有一个漂亮的打印机。试试这个吧,print json.dumps(s, indent=4)。
>>> s = {'pk': 5678, 'model': 'trial model', 'fields': {'brief_title': 'a short title', 'trial_id': 1234}}
>>> print json.dumps(s, indent=4)
{
"pk": 5678,
"model": "trial model",
"fields": {
"brief_title": "a short title",
"trial_id": 1234
}
}https://stackoverflow.com/questions/8024769
复制相似问题