【发布时间】: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