Json 到 Python 中的 RDF/XML 文件
Json to RDF/XML file in Python
我正在尝试保存这个 json 对象:
[
{
"patient_id": "59b70b0e-51cb-4215-b65a-db470067c8de"
},
[
{
"tel_1_preferred_p": true,
"adr_region": "OK",
"tel_2_number": "800-979-6786",
"__modelname__": "Demographics",
"adr_city": "Bixby",
"ethnicity": null,
"adr_postalcode": "74008",
"name_family": "John",
"name_middle": null,
"tel_1_type": "h",
"tel_2_type": "c",
"name_prefix": null,
"email": "william.robinson@example.com",
"name_given": "Smith",
"adr_street": "23 Church Rd",
"bday": "1965-08-09",
"__documentid__": "65cd1101-b047-4ce5-afc0-778d033229ca",
"tel_1_number": "800-870-3011",
"preferred_language": "EN",
"gender": "male",
"name_suffix": null,
"tel_2_preferred_p": true,
"race": null,
"adr_country": "USA"
}
],
[
{
"startDate": "2014-10-02T00:00:00Z",
"name_identifier": "195967001",
"name_system": null,
"notes": "None",
"name_title": "Asthma",
"__modelname__": "Problem",
"endDate": "2014-10-13T00:00:00Z",
"__documentid__": "eaba4e92-3aaf-4d8c-a7db-8cfd563290c6"
},
{
"startDate": "2014-12-02T00:00:00Z",
"name_identifier": "161155000",
"name_system": null,
"notes": "None",
"name_title": "School problem (finding)",
"__modelname__": "Problem",
"endDate": "2014-12-14T00:00:00Z",
"__documentid__": "3058b12d-434d-48da-8b14-05dd485946cb"
},
{
"startDate": "2008-07-29T00:00:00Z",
"name_identifier": "185903001",
"name_system": "http://purl.bioontology.org/ontology/SNOMEDCT/",
"notes": null,
"name_title": "Needs influenza immunization",
"__modelname__": "Problem",
"endDate": "2010-09-13T00:00:00Z",
"__documentid__": "d982c117-43c3-407f-bd9e-27dc59008938"
}
],
[
{
"name": "Amputation of the foot (procedure)",
"comments": null,
"provider_institution": null,
"date_performed": "2014-10-22T00:00:00Z",
"name_value": "None",
"__modelname__": "Procedure",
"__documentid__": "221df5bd-16a8-4763-9827-06fb305b91e5",
"provider_name": null,
"name_abbrev": "180030006",
"name_type": "http://purl.bioontology.org/ontology/SNOMEDCT/",
"location": null
}
]
]
到 XML 文件或 python 代码中的 RDF。我没有找到关于 rdf 的 post,所以我尝试了 XML。我还尝试将列表更改为像这样的字典 post:
Python Json loads() returning string instead of dictionary?
但它不起作用。在 views.py 我有:
fileroot = '[{"patient_id":"'+record_id+'"},'+content0+','+content1+','+content2+']'
jsonData = simplejson.loads(fileroot)
它returns 'list'对象没有属性'items'。如果我将其更改为:
fileroot = '[{"patient_id":"'+record_id+'"},'+content0+','+content1+','+content2+']'
jsonData = simplejson.loads(fileroot)[0]
它returns没什么。
我也尝试过 link:
https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch06s05.html
但它不再工作了。
这会将您的 json 对象翻译成 xml
import simplejson
content = simplejson.loads(YourJsonObject)
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import tostring
def dict_to_xml(tag, d):
'''
Turn a simple dict of key/value pairs into XML
'''
elem = Element(tag)
for key, val in d.items():
child = Element(key)
child.text = str(val)
elem.append(child)
return elem
for i in range(1,len(content)):
e = dict_to_xml('SomeNode',content[i][0])
# print e
print tostring(e)
我正在尝试保存这个 json 对象:
[
{
"patient_id": "59b70b0e-51cb-4215-b65a-db470067c8de"
},
[
{
"tel_1_preferred_p": true,
"adr_region": "OK",
"tel_2_number": "800-979-6786",
"__modelname__": "Demographics",
"adr_city": "Bixby",
"ethnicity": null,
"adr_postalcode": "74008",
"name_family": "John",
"name_middle": null,
"tel_1_type": "h",
"tel_2_type": "c",
"name_prefix": null,
"email": "william.robinson@example.com",
"name_given": "Smith",
"adr_street": "23 Church Rd",
"bday": "1965-08-09",
"__documentid__": "65cd1101-b047-4ce5-afc0-778d033229ca",
"tel_1_number": "800-870-3011",
"preferred_language": "EN",
"gender": "male",
"name_suffix": null,
"tel_2_preferred_p": true,
"race": null,
"adr_country": "USA"
}
],
[
{
"startDate": "2014-10-02T00:00:00Z",
"name_identifier": "195967001",
"name_system": null,
"notes": "None",
"name_title": "Asthma",
"__modelname__": "Problem",
"endDate": "2014-10-13T00:00:00Z",
"__documentid__": "eaba4e92-3aaf-4d8c-a7db-8cfd563290c6"
},
{
"startDate": "2014-12-02T00:00:00Z",
"name_identifier": "161155000",
"name_system": null,
"notes": "None",
"name_title": "School problem (finding)",
"__modelname__": "Problem",
"endDate": "2014-12-14T00:00:00Z",
"__documentid__": "3058b12d-434d-48da-8b14-05dd485946cb"
},
{
"startDate": "2008-07-29T00:00:00Z",
"name_identifier": "185903001",
"name_system": "http://purl.bioontology.org/ontology/SNOMEDCT/",
"notes": null,
"name_title": "Needs influenza immunization",
"__modelname__": "Problem",
"endDate": "2010-09-13T00:00:00Z",
"__documentid__": "d982c117-43c3-407f-bd9e-27dc59008938"
}
],
[
{
"name": "Amputation of the foot (procedure)",
"comments": null,
"provider_institution": null,
"date_performed": "2014-10-22T00:00:00Z",
"name_value": "None",
"__modelname__": "Procedure",
"__documentid__": "221df5bd-16a8-4763-9827-06fb305b91e5",
"provider_name": null,
"name_abbrev": "180030006",
"name_type": "http://purl.bioontology.org/ontology/SNOMEDCT/",
"location": null
}
]
]
到 XML 文件或 python 代码中的 RDF。我没有找到关于 rdf 的 post,所以我尝试了 XML。我还尝试将列表更改为像这样的字典 post:
Python Json loads() returning string instead of dictionary?
但它不起作用。在 views.py 我有:
fileroot = '[{"patient_id":"'+record_id+'"},'+content0+','+content1+','+content2+']'
jsonData = simplejson.loads(fileroot)
它returns 'list'对象没有属性'items'。如果我将其更改为:
fileroot = '[{"patient_id":"'+record_id+'"},'+content0+','+content1+','+content2+']'
jsonData = simplejson.loads(fileroot)[0]
它returns没什么。 我也尝试过 link:
https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch06s05.html
但它不再工作了。
这会将您的 json 对象翻译成 xml
import simplejson
content = simplejson.loads(YourJsonObject)
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import tostring
def dict_to_xml(tag, d):
'''
Turn a simple dict of key/value pairs into XML
'''
elem = Element(tag)
for key, val in d.items():
child = Element(key)
child.text = str(val)
elem.append(child)
return elem
for i in range(1,len(content)):
e = dict_to_xml('SomeNode',content[i][0])
# print e
print tostring(e)