关键信息临时中转╭(╯^╰)╮,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import xml.etree.ElementTree as ET import re def handle_version_str(src_str): # 获取匹配的自组数据 m = re.search(r'(.*)(V\d{3}R\d{3}C\d{2})(.*)', src_str) if m is not None: ver = m.group(2) print(ver) # 通过获取的匹配内容,将其删除 dst_str = re.sub(ver, '', src_str) else: return None return dst_str def create_result_xml(): model = '''<data><country name="China">test</country></data>''' root = ET.fromstring(model) # 添加子节点 sub_attrib = {"location": "cn", "color":"black"} root.append("sub1", sub_attrib) # 如何通过ET写入不是parse方式创建的ET到一个xml文件 tree = ET.ElementTree(root) tree.write('result.xml') if __name__ == "__main__": ver_str = "Hello_V001R002C20_Release" dst_str = handle_version_str(ver_str) print(dst_str) create_result_xml() |