Python 中的 Json 到 RDF/XML 文件 [英] Json to RDF/XML file in Python

查看:79
本文介绍了Python 中的 Json 到 RDF/XML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存这个 json 对象:

<预><代码>[{患者 ID":59b70b0e-51cb-4215-b65a-db470067c8de"},[{tel_1_preferred_p":真,"adr_region": "好的","tel_2_number": "800-979-6786","__modelname__": "人口统计","adr_city": "比克斯比",种族":空,"adr_postalcode": "74008","name_family": "约翰",name_middle":空,"tel_1_type": "h","tel_2_type": "c",name_prefix":空,"email": "william.robinson@example.com","name_given": "史密斯","adr_street": "23 Church Rd","生日": "1965-08-09","__documentid__": "65cd1101-b047-4ce5-afc0-778d033229ca","tel_1_number": "800-870-3011","preferred_language": "EN","性别": "男",name_suffix":空,tel_2_preferred_p":真,种族":空,"adr_country": "美国"}],[{"startDate": "2014-10-02T00:00:00Z","name_identifier": "195967001",name_system":空,"notes": "无","name_title": "哮喘","__modelname__": "问题","endDate": "2014-10-13T00:00:00Z","__documentid__": "eaba4e92-3aaf-4d8c-a7db-8cfd563290c6"},{"startDate": "2014-12-02T00:00:00Z","name_identifier": "161155000",name_system":空,"notes": "无","name_title": "学校问题(发现)","__modelname__": "问题","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/",注释":空,"name_title": "需要接种流感疫苗","__modelname__": "问题","endDate": "2010-09-13T00:00:00Z","__documentid__": "d982c117-43c3-407f-bd9e-27dc59008938"}],[{"name": "足部截肢(程序)",评论":空,provider_institution":空,"date_performed": "2014-10-22T00:00:00Z","name_value": "无","__modelname__": "程序","__documentid__": "221df5bd-16a8-4763-9827-06fb305b91e5",provider_name":空,"name_abbrev": "180030006","name_type": "http://purl.bioontology.org/ontology/SNOMEDCT/",位置":空}]

]

到 Python 代码中的 XML 文件或 RDF.我没有找到关于 rdf 的帖子,所以我尝试了 XML.我还尝试将列表更改为 dict 就像这篇文章:

Python Json 加载()返回字符串而不是字典?

但它不起作用.在 views.py 我有:

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

它返回没有属性items"的list"对象.如果我将其更改为:

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

它什么都不返回.我也从这个链接试过:

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

但它不再起作用了.

解决方案

这会将您的 json 对象转换为 xml

import simplejsoncontent = simplejson.loads(YourJsonObject)从 xml.etree.ElementTree 导入元素从 xml.etree.ElementTree 导入到字符串def dict_to_xml(tag, d):'''将简单的键/值对字典转换为 XML'''elem = 元素(标签)对于键,d.items() 中的 val:孩子 = 元素(键)child.text = str(val)elem.append(child)返回元素对于范围内的 i(1,len(content)):e = dict_to_xml('SomeNode',content[i][0])# 打印 e打印字符串(e)

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.

解决方案

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)

这篇关于Python 中的 Json 到 RDF/XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆