gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:58:12 25 4
gpt4 key购买 nike

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

我已经走到这一步了:

'{ {\n "pk":2,\n "model": trials.conditions,\n "fields": {\n "trial_id": NCT00109798,\n "keyword": 大脑和中枢神经系统肿瘤,\n }{\n "pk":3,\n "model": trials.conditions,\n "fields": {\n "trial_id": NCT00109798,\n "keyword": 淋巴瘤,\n } {\n "pk": 2,\n "model": trials.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": trials.keyword,\n\t\t"fields": {\n\t\t"trial_id": NCT00109798,\n\t\t"keyword": primary central nervous system non-Hodgkin lymphoma,\n\t\t}\n\t\t

...很多行之后...

完成研究治疗后,患者每 3 个月随访 1 年,每\n 4 个月随访 1 年,然后每 6 个月随访 3 年。\n\n 预计累积:总共 6-25本研究将招募患者。\n,\n“overall_status”:招募,\n“阶段”:第 2 阶段,\n“注册”:25,\n“study_type”:介入,\n“条件”: 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 “关键字”:2,3,\n “条件网格”:,\n }\n\n {\n “pk”:测试数据,\n “模型”:trials.contact,\n “字段”:{\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,
}

如有任何建议,我们将不胜感激。

最佳答案

替代 json.dumps 缩进参数:

Python 在 http://docs.python.org/library/pprint.html 有一个 pretty-print .它使用起来非常简单,但只能漂亮地打印 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'}

关于python - 在 Python 中使用换行符和制表符格式化字符串输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8024769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com