使用Python中的换行符和制表符格式化字符串输出?

时间:2011-11-06 02:12:23

标签: python django string formatting fixture

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

我已经能够做到这一点:

'{{\ n“pk”:2,\ n“model”:trials.conditions,\ n“fields”:{\ n“trial_id”:NCT00109798,\ n“关键字”:脑和中枢神经系统肿瘤,\ n} {\ n“pk”:3,\ n“model”:trials.conditions,\ n“fields”:{\ n“trial_id”:NCT00109798,\ n“关键字”:淋巴瘤,\ n} {\ n“pk”:2,\ n“model”:trials.criteria,\ n“fields”:{\ n“trial_id”:NCT00109798,\ n“性别”:两者,\ n“minimum_age”:18年,\ n“maximum_age”:N / A,\ n“healthy_volunteers”:否,\ n“textblock”:,\ n} \ n \ t \ t“pk”:2,\ n \ t \ t“model” :trials.keyword,\ n \ t \ t“字段”:{\ n \ t \ t“trial_id”:NCT00109798,\ n \ t \ t“关键字”:原发性中枢神经系统非霍奇金淋巴瘤,\ n \吨\吨} \ n \吨\吨

......很多行后来......

完成研究治疗后,患者每3个月随访1年,每4个月随访1年,然后每6个月随访3年。\ n \ n预计接种:总共6-25患者将参与本研究。\ n,\ n“overall_status”:招募,\ n“阶段”:阶段2,\ n“注册”:25,\ n“study_type”:介入,\ n“条件”: 2,3,\ n“标准”:1,\ n“overall_contact”:testdata,\ n“location”:4,\ n“lastchanged_date”:2010年3月31日,\ n“firstreceived_date”:2005年5月3日, \ n“关键字”:2,3,\ n“condition_mesh”:,\ n} \ n \ n {\ n“pk”:testdata,\ n“model”:trials.contact,\ n“fields”:{ \ 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,
    }

非常感谢任何建议。

2 个答案:

答案 0 :(得分: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'}

答案 1 :(得分: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
    }
}