python: json transform xml

python read and write json

json

  1. JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of ECMAScript. JSON uses a text format that is completely language-independent, but also uses habits similar to the C language family (including C, C++, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language. It is easy for people to read and write, but also easy for machine to parse and generate (usually used to improve the network transmission rate).
  2. JSON is composed of list and dict in python.
  3. The json module provides four functions: dumps, dump, loads, load
  • dumps: Convert dictionaries in python to strings
  • loads: convert a string to a dictionary
  • dump: write data to json file
  • load: open the file and convert the string to a data type

Reference: python reads and writes json files .

python read and write xml

#方式一
file = open("路径",'r',encoding = 'utf-8')
#方式二
with open("路径",'r',encoding = 'utf-8')as file_obj:
	语句块

Recommend the second one. Because this method can close the file under any circumstances, and the organization is clear.

python: json transform xml

  1. Directly use for loop conversion
  2. Use library function conversion: dicttoxml
  • Install the library dicttoxml, which converts the dictionary in python to xml format, combined with the json.loads() function, can convert json content to xml format content.

pip install dicttoxml

  • Use custom_root to customize the name of the root node in the dicttoxml method; item_func to customize the name of the item node; attr_type=False to choose whether to add a type description, this article chooses not to add.
  • Upload code
import os
from json import loads
from dicttoxml import dicttoxml
from xml.dom.minidom import parseString


def 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 file
    with 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_dir
    if(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 file
    j_path = "F:/清影科技/work/jsontoxml/json/test.json"
    x_path = "F:/清影科技/work/jsontoxml/json/test.xml"
    jsonToXml(j_path,x_path)

    #transfer multi files
    j_dir = "F:/清影科技/work/jsontoxml/json/"
    x_dir = "F:/清影科技/work/jsontoxml/xml/"
    json_to_xml(j_dir, x_dir)
  • effect:
    Insert picture description here
    Insert picture description here

Small tool: Convert all json files in this folder to xml files

python: xml conversion json

Follow-up supplement

Guess you like

Origin blog.csdn.net/u013894391/article/details/103006594