Python Xml文件添加字节属性的方法

时间:2021-05-22

实例如下所示:

from xml.etree.cElementTree import ElementTree,Elementimport xlrdimport redef read_xlsx(xlsx_path): workbook = xlrd.open_workbook(xlsx_path) booksheet = workbook.sheet_by_name("Sheet1") p = list() row_data = [] for row in range(booksheet.nrows): for col in range(booksheet.ncols): cel = booksheet.cell(row, col) val = cel.value try: val = cel.value val = re.sub(r'\s+', '', val) except: pass if type(val) == float: val = int(val) else: val = str(val) row_data.append(val) p.append(row_data) return pdef read_xml(Int_path): tree = ElementTree() tree.parse(Int_path) return treedef write_xml(tree, out_path): tree.write(out_path,encoding="utf-8",xml_declaration=True) '''tree.write('output.xml')'''def if_match(node,k_name,k_val): c = node.get(k_name) if node.get(k_name) == k_val: return True return Falsedef find_nodes(tree,path): return tree.findall(path)def get_node_by_keyvalue(nodelist,key_name,key_val): result_nodes=[] for node in nodelist: if if_match(node,key_name,key_val): result_nodes.append(node) return result_nodesdef add_node_properties(nodelist,add_key,add_properties): for node in nodelist: node.set(add_key,add_properties) return nodeif __name__ == "__main__": tree = read_xml("./ContentList.xml") nodes = find_nodes(tree,"Content") P = read_xlsx("./ServiceId.xlsx") a = P[0] i = 0 while i < 80: result_nodes = get_node_by_keyvalue(nodes , "AssetId" , a[i]) add_node_properties(result_nodes,"ServiceId",a[i+1]) i = i + 2 write_xml(tree,'./ContentList1.xml')

以上这篇Python Xml文件添加字节属性的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章