Python Xml文件添加字节属性的方法
作者:风吹群起PP凉 时间:2023-08-27 03:48:31
实例如下所示:
from xml.etree.cElementTree import ElementTree,Element
import xlrd
import re
def 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 p
def read_xml(Int_path):
tree = ElementTree()
tree.parse(Int_path)
return tree
def 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 False
def 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_nodes
def add_node_properties(nodelist,add_key,add_properties):
for node in nodelist:
node.set(add_key,add_properties)
return node
if __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')
来源:https://blog.csdn.net/NicoleZhang2012/article/details/73650255
标签:Python,Xml,字节,属性
0
投稿
猜你喜欢
django2.0扩展用户字段示例
2023-08-31 11:10:10
python 通过SMSActivateAPI 获取验证码的步骤
2022-04-23 15:16:43
一些关于SQL2005+ASP.NET2.0的问题
2007-09-23 13:01:00
python 实现朴素贝叶斯算法的示例
2023-12-09 12:59:11
Oracle过程与函数的区别分析
2024-01-15 20:15:37
Access数据库出现0x80004005问题的解决方法
2008-11-28 14:25:00
儿童学习python的一些小技巧
2021-10-14 05:09:40
Python使用pickle模块报错EOFError Ran out of input的解决方法
2021-06-03 02:56:57
利用Tkinter(python3.6)实现一个简单计算器
2022-07-16 10:42:30
vue多页面项目实现版本快照功能示例详解
2024-04-28 09:23:25
python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法
2021-09-15 16:28:22
4行Python代码生成图像验证码(2种)
2021-02-08 06:08:03
Pycharm在指定目录下生成文件和删除文件的实现
2022-04-12 20:00:28
js replace()去除代码中空格的实例
2024-04-29 13:36:26
mysql学习笔记之基础知识
2024-01-13 08:27:16
pygame实现方块动画实例讲解
2022-11-01 13:37:46
浅谈PHP的数据库接口和技术
2024-05-02 17:13:49
mysql生成随机字符串函数分享
2024-01-28 14:16:08
mysql锁表和解锁语句分享
2024-01-13 12:08:36
Linux下编译安装MySQL-Python教程
2021-05-03 05:05:40