多进程解析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文件及入库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-10XMLHTTPRequest项目实战:从入门到上手
- 2024-02-26lxml not found please install it
- 2022-09-16使用Druid数据源-开启监控-xml的方式-配置类的方式
- 2022-09-08IDEA中pom.xml配置文件依赖文件版本号报红的最有效解决办法
- 2022-09-05如何更换项目中web.xml的版本
- 2022-09-02使用dom4j xml解析文件数据
- 2022-08-24可扩展标记语言——XML
- 2022-08-23mybatis 配置文件mybatis.xml的加载过程
- 2022-08-162022最新有效 哔哩哔哩Bilibili手机端.m4s文件缓存转.mp4教程 支持每个视频单独一个文件夹 支持转换xml弹幕
- 2022-08-15mybatis_8_在pom.xml中添加lombok