日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python:json转xml

發(fā)布時(shí)間:2023/12/20 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python:json转xml 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

      • python讀寫(xiě)json
        • json
      • python讀寫(xiě)xml
      • python:json轉(zhuǎn)xml
      • python:xml轉(zhuǎn)json

python讀寫(xiě)json

json

  • JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式。它基于ECMAScript的一個(gè)子集。JSON采用完全獨(dú)立于語(yǔ)言的文本格式,但是也使用了類(lèi)似于C語(yǔ)言家族的習(xí)慣(包括C、C++、Java、JavaScript、Perl、Python等)。這些特性使JSON成為理想的數(shù)據(jù)交換語(yǔ)言。易于人閱讀和編寫(xiě),同時(shí)也易于機(jī)器解析和生成(一般用于提升網(wǎng)絡(luò)傳輸速率)。
  • JSON在python中分別由list和dict組成。
  • json模塊提供了四個(gè)功能:dumps,dump,loads,load
    • dumps: 將python中的字典轉(zhuǎn)換為字符串
    • loads: 將字符串轉(zhuǎn)換為字典
    • dump: 將數(shù)據(jù)寫(xiě)入json文件中
    • load: 把文件打開(kāi),并把字符串變換為數(shù)據(jù)類(lèi)型

    參考:python讀寫(xiě)json文件.

    python讀寫(xiě)xml

    #方式一 file = open("路徑",'r',encoding = 'utf-8') #方式二 with open("路徑",'r',encoding = 'utf-8')as file_obj:語(yǔ)句塊

    推薦第二種。因?yàn)樵摲N方式可以在任何情況下關(guān)閉文件,且條理清晰。

    python:json轉(zhuǎn)xml

  • 直接自己擼個(gè)for循環(huán)轉(zhuǎn)換
  • 使用庫(kù)函數(shù)轉(zhuǎn)換:dicttoxml
    • 安裝庫(kù)dicttoxml,該庫(kù)是將python中的字典轉(zhuǎn)換為xml格式,結(jié)合json.loads()函數(shù)能給將json內(nèi)容轉(zhuǎn)換為xml格式的內(nèi)容。

    pip install dicttoxml

    • dicttoxml方法中使用custom_root自定義根節(jié)點(diǎn)名稱(chēng);item_func自定義項(xiàng)目節(jié)點(diǎn)名稱(chēng);attr_type=False選擇是否添加類(lèi)型說(shuō)明,該文選擇不添加。
    • 上代碼
    import os from json import loads from dicttoxml import dicttoxml from xml.dom.minidom import parseStringdef jsonToXml(json_path, xml_path):#@abstract: transfer json file to xml file#json_path: complete path of the json file#xml_path: complete path of the xml filewith open(json_path,'r',encoding='UTF-8')as json_file:load_dict=loads(json_file.read())#print(load_dict)my_item_func = lambda x: 'Annotation'xml = dicttoxml(load_dict,custom_root='Annotations',item_func=my_item_func,attr_type=False)dom = parseString(xml)#print(dom.toprettyxml())#print(type(dom.toprettyxml()))with open(xml_path,'w',encoding='UTF-8')as xml_file:xml_file.write(dom.toprettyxml())def json_to_xml(json_dir, xml_dir):#transfer all json file which in the json_dir to xml_dirif(os.path.exists(xml_dir)==False):os.makedirs(xml_dir)dir = os.listdir(json_dir)for file in dir:file_list=file.split(".")if(file_list[-1] == 'json'):jsonToXml(os.path.join(json_dir,file),os.path.join(xml_dir,file_list[0]+'.xml')) if __name__ == '__main__':#trandfer singal filej_path = "F:/work/jsontoxml/json/test.json"x_path = "F:/work/jsontoxml/json/test.xml"jsonToXml(j_path,x_path)#transfer multi filesj_dir = "F:/work/jsontoxml/json/"x_dir = "F:/work/jsontoxml/xml/"json_to_xml(j_dir, x_dir)
    • 效果:

    小工具:將本文件夾內(nèi)的所有json文件轉(zhuǎn)換為xml文件

    python:xml轉(zhuǎn)json

    后續(xù)補(bǔ)充

    總結(jié)

    以上是生活随笔為你收集整理的python:json转xml的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。