import xml.etree.ElementTree as ET import traceback import pandas as pd import datetime filehoder=r'D:\myproject_python\AOI\xml_AOI -1\xml_AOI' filename=['V-NC419764','V-NC420305','NC421886'] with open(r'D:\myproject_python\AOI\xml_AOI -1\xml_AOI\0_V-NC419764.txt',"r") as f: file_section_list=f.read() f.close() print(file_section_list) def cutXmlSentences(file_str): """ 说明:一个xml文件中包含多个html段,进行分割. :param file: :return: """ final_list = [] output = "" str_list = file_str.split("\n") for i in range(len(str_list)): if str_list[i] != "\n": output += str_list[i] else: final_list.append(output) output = "" final_list.append(output) tmp_list = list(filter(lambda s: s != '' and s != ' ' and s != '\n', final_list)) return tmp_list def traversexml(element): """ 说明:遍历element所有子节点. :param element: :return: """ # print (len(element)) if len(element) > 0: for child in element: print(child.tag, "----", child.attrib) traversexml(child) def ReadXml(xml_string): """ 说明:解析xml文件成root. :param xml_string: :return: """ root = None if (xml_string): try: # print("xmlstring provided") root = ET.fromstring(xml_string) except Exception: # as e: print(xml_string) print("parse xmlstring failed!") finally: return root child_root = ReadXml(file_section_list) final_data_dict = {} for header in child_root.findall('header'): #这些元素只出现一次 for serialnumber in header.findall("serialnumber"): final_data_dict['boardid'] = serialnumber.text.strip("V-") for dt in header.findall("datetime"): date_time = datetime.datetime.strptime(dt.text.replace(".000+08:00", "").replace("T", " "),"%Y-%m-%d %H:%M: