1

我试图从大量文件中提取一些数据并将它们转换为特定的(JSON)格式,以便使用 Django Fixtures 导入数据库

我已经能够做到这一点:

'{ {\n "pk":2,\n "model": trial.conditions,\n "fields": {\n "trial_id": NCT00109798,\n "keyword": Brain and Central Nervous System Tumors,\ n }{\n "pk":3,\n "model": trial.conditions,\n "fields": {\n "trial_id": NCT00109798,\n "keyword": 淋巴瘤,\n }{\n "pk": 2,\n "model": trial.criteria,\n "fields": {\n "trial_id": NCT00109798,\n "gender": Both,\n "minimum_age": 18 年,\n "maximum_age": N/A,\n "healthy_volunteers": 否,\n "textblock": ,\n }\n\t\t"pk":2,\n\t\t"model": trial.keyword,\n\t\t"fields": {\n\t\t"trial_id": NCT00109798,\n\t\t"keyword ": 原发性中枢神经系统非霍奇金淋巴瘤,\n\t\t}\n\t\t

......很多行之后......

研究治疗完成后,患者每 3 个月随访 1 年,每\n 4 个月随访 1 年,然后每 6 个月随访 3 年。\n\n 预计累积:总共 6-25 名患者将为本研究累积。\n ,\n "overall_status": 招募中,\n "phase": 第 2 阶段,\n "enrollment": 25,\n "study_type": 介入,\n "condition": 2,3 ,\n "criteria": 1,\n "overall_contact": testdata,\n "location": 4,\n "lastchanged_date": March 31, 2010,\n "firstreceived_date": May 3, 2005,\n "关键字": 2,3,\n "condition_mesh": ,\n }\n \n {\n "pk": testdata,\n "model": trial.contact,\n "fields": {\n "trial_id": NCT00109798,\n "last_name": Pamela Z. New, MD,\n "phone": ,\n "email": ,\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,
    }

任何建议将不胜感激。

4

2 回答 2

3

替代 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-09T17:19:47.667 回答
1

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
    }
}
于 2011-11-06T02:32:48.400 回答