Json to RDF/XML file in Python

十年热恋 提交于 2021-01-28 12:31:34

问题


I am trying to save this json object:

[
{
    "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
    }
]

]

to an XML file or RDF in python code. I didn't find posts about rdf so I tried for XML. I tried also to change the list to dict like in this post:

Python Json loads() returning string instead of dictionary?

but it is not working. In views.py I have:

 fileroot = '[{"patient_id":"'+record_id+'"},'+content0+','+content1+','+content2+']'
jsonData = simplejson.loads(fileroot)

It returns 'list' object has no attribute 'items'. And if I change it to :

  fileroot = '[{"patient_id":"'+record_id+'"},'+content0+','+content1+','+content2+']'
jsonData = simplejson.loads(fileroot)[0]

It returns nothing. I also tried from this link:

https://www.safaribooksonline.com/library/view/python-cookbook-3rd/9781449357337/ch06s05.html

but it is not working again.


回答1:


This will tranlsate your json object to 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)


来源:https://stackoverflow.com/questions/29030231/json-to-rdf-xml-file-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!