多进程解析xml文件及入库

2021/12/14 7:17:49

本文主要是介绍多进程解析xml文件及入库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

xml文件解析成json字符串

import json
improt xmltodict


def parse(path):
    with open(path, encoding="utf8) as f:
        data = f.read()
    xmlparse = xmltodict.parse(data)
    res = json.dumps(xmlparse)
    return res

解析、插入、多进程实例代码

# -*- coding: utf-8 -*-


import os
import json
import xmltodict
import pymysql
from concurrent.futures import ThreadPoolExecutor


def read_xml(path):
    p_list = []
    for roots, _, names in os.walk(path):
        for name in names:
            if name.endswith(".xml"):
                p_list.append(os.path.join(roots, name))
    return p_list


def parse(path):
    """
    解析xml文件
    :param path:xml文件地址
    :return: json字符串列表
    """
    with open(path, encoding="utf8") as f:
        data = f.read()
    xmlparse = xmltodict.parse(data)
    jsonstr = json.dumps(xmlparse)
    res = json.loads(jsonstr)
    id = res["sapphire"]["dcarRecord"]["patientInfo"]["identifier"]["id"]["@V"]
    name = res["sapphire"]["dcarRecord"]["patientInfo"]["name"]["@use"]
    path_ = path.split("\\")[-1]
    res_ = [path_, id, name]
    insert(res_)


def insert(res):
    """
    xml信息入库
    :param res:
    :return:
    """
    host = "***"
    user = "***"
    password = "***"
    db = "***"

    conn = pymysql.connect(host=host, user=user, password=password, db=db)
    # sql = """insert into ecg values('{}', '{}', '{}')""".format(res[0], res[1], res[2])
    # sql = """select * from ecg"""
    sql = """insert into ecg values ('3080637512_201806051059551.xml', '3080637512_201806051059515', '张新粉1')"""
    print(sql)
    with conn.cursor() as a:
        a.execute(sql)
        conn.commit()
        line = a.fetchone()
    print(res[0], "插入成功")


def main(path):
    """
    多进程
    :param path:
    :return:
    """
    p_list = read_xml(path)
    with ThreadPoolExecutor(max_workers=1) as pool:
        pool.map(parse, p_list)


if __name__ == '__main__':
    path = r"G:\Desktop\text"
    main(path)



这篇关于多进程解析xml文件及入库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程