【问题标题】:formatting string output with linebreaks and tabs in Python?在 Python 中使用换行符和制表符格式化字符串输出?
【发布时间】:2011-11-06 02:12:23
【问题描述】:

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

我已经做到了这一点:

'{ {\n "pk":2,\n "model": trial.conditions,\n "fields": {\n "trial_id": NCT00109798,\n "keyword": 大脑和中枢神经系统肿瘤,\n }{\n "pk":3,\n "model": trial.conditions,\n "fields": {\n "trial_id": NCT00109798,\n "keyword": Lymphoma,\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": Interventional,\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": 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,
    }

任何建议将不胜感激。

【问题讨论】:

  • 您可能会查看源代码code.djangoproject.com/browser/django/trunk/django/core/… 中管理命令“dumpdata”的代码,因为它可以选择缩进输出,我收集到您正在尝试这样做
  • 格式和缩进在 JSON 中是无关紧要的。
  • @pastylegs 不幸的是,数据不是来自数据库,所以我不能使用转储数据。我获取一个 XML 文件并提取相关字段并使用 python 以 Fixture JSON 格式输出它们。我基本上是返回一个带有换行符的巨大字符串。

标签: python django string formatting fixture


【解决方案1】:

替代 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'}

【讨论】:

  • 谢谢,这正是我要找的。​​span>
【解决方案2】:

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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多