import csv from xml.etree.ElementTree import Element, ElementTree def csv2Xml(fname): with open(fname) as f: reader = csv.reader(f) headers = reader.__next__() # 获取csv的首行作为tag标签 root = Element('data') # 创建根节点 for row in reader: # 遍历csv文件 eRow = Element('row') # 每行作为一个根节点的子节点 root.append(eRow) for tag, text in zip(headers, row): # 遍历每行的每个数据 e = Element(tag) # 获取tag e.text = text # 获取值 eRow.append(e) # 添加到row节点下 return ElementTree(root) et = csv2Xml(r"C:\Users\admin\Desktop\test.csv") et.write(r"C:\Users\admin\Desktop\test.xml")
python3写xml数据
最新推荐文章于 2020-12-19 06:47:37 发布