# -*- coding:utf-8 -*-
#! python 3.9.7
'''
因为项目中有几十个xml配置文件要修改,每个文件有好几百行,手动修改工作量大还容易出错。
所以通过python脚本实现一个小工具,读取xml文件内容,修改属性的value值后,再重新写入xml文件。
该代码通过参考网络资源,临时拼凑起来的。
'''
import xml.dom.minidom # dom方式解析xml文件
import re,os # 正则表达式替换字符串
# 替换xml文件中的字符串
def alter(file,old_str,new_str):
with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
#with open(file, "r") as f1,open("%s.bak" % file, "w") as f2:
for line in f1:
f2.write(re.sub(old_str,new_str,line))
f1.close()
f2.close()
os.remove(file)
os.rename("%s.bak" % file, file)
# 修改xml文件中属性value值
def update_xml_cfg(input_train_number):
dom = xml.dom.minidom.parse("car_1.xml")
root = dom.documentElement
#print("nodeName:",root.nodeName)
#print("nodeValue:",root.nodeValue)
#print(root.getAttribute("name"))
root.setAttribute("name", str(input_train_number))
train_number = input_train_number # 列车号
listNVR = root.getElementsByTagName('nvr')
car_number = 0 # 车厢号
for item_nvr in listNVR:
list_param = item_nvr.getElementsByTagName('param')
# 修改nvr的名称、国标ID和IP地址
#print(list_param[0].getAttribute("name"),":",list_param[0].getAttribute("value"))
#print(list_param[1].getAttribute("name"),":",list_param[1].getAttribute("value"))
#print(list_param[2].getAttribute("name"),":",list_param[2].getAttribute("value"))
car_number = car_number + 1
nvr_name = str(train_number) + "_nvr_" + str(car_number)
print("nvr_name=", nvr_name)
nvr_gbid = "330196240011809" + str(train_number-10) + str(car_number) + "00"
print("nvr_gbid=", nvr_gbid)
nvr_ip = "10.32." + str(train_number+63) + "." + str(car_number+132)
print("nvr_ip=", nvr_ip)
list_param[0].setAttribute("value", nvr_name)
list_param[1].setAttribute("value", nvr_gbid)
list_param[2].setAttribute("value", nvr_ip)
# 修改摄像机的国标ID和名称
loop_count = 0
camera_number = 0 # 摄像机号
camera_gbid = ""
camera_name = ""
camera_name_cctv = ""
if (car_number == 1) or (car_number == 2):
camera_number = 0
elif (car_number == 3) or (car_number == 4):
camera_number = 12
elif (car_number == 5) or (car_number == 6):
camera_number = 20
list_camera = item_nvr.getElementsByTagName('param')
for item_camera in list_camera:
loop_count = loop_count + 1
if loop_count > 5:
if (item_camera.getAttribute("name") == 'DeviceID'):
camera_number = camera_number +1
#print(item_camera.getAttribute("name"),":",item_camera.getAttribute("value"))
if camera_number < 10:
camera_gbid = "330196240013209" + str(train_number-10) + str(car_number) + str(0) + str(camera_number)
else:
camera_gbid = "330196240013209" + str(train_number-10) + str(car_number) + str(camera_number)
item_camera.setAttribute("value", camera_gbid)
print("camera_gbid=", camera_gbid)
elif (item_camera.getAttribute("name") == 'Name'):
#print(item_camera.getAttribute("name"),":",item_camera.getAttribute("value"))
# 1车到6车摄像机在监控屏上的描述信息
if (camera_number == 1):
camera_name_cctv = "1S1"
elif (camera_number == 2):
camera_name_cctv = "1S2"
elif (camera_number == 3):
camera_name_cctv = "1A"
elif (camera_number == 4):
camera_name_cctv = "1B"
elif (camera_number == 5):
camera_name_cctv = "1C"
elif (camera_number == 6):
camera_name_cctv = "1D"
elif (camera_number == 7):
camera_name_cctv = "2A"
elif (camera_number == 8):
camera_name_cctv = "2B"
elif (camera_number == 9):
camera_name_cctv = "2C"
elif (camera_number == 10):
camera_name_cctv = "2D"
elif (camera_number == 11):
camera_name_cctv = "2P1"
elif (camera_number == 12):
camera_name_cctv = "2P2"
elif (camera_number == 13):
camera_name_cctv = "3A"
elif (camera_number == 14):
camera_name_cctv = "3B"
elif (camera_number == 15):
camera_name_cctv = "3C"
elif (camera_number == 16):
camera_name_cctv = "3D"
elif (camera_number == 17):
camera_name_cctv = "4A"
elif (camera_number == 18):
camera_name_cctv = "4B"
elif (camera_number == 19):
camera_name_cctv = "4C"
elif (camera_number == 20):
camera_name_cctv = "4D"
elif (camera_number == 21):
camera_name_cctv = "5A"
elif (camera_number == 22):
camera_name_cctv = "5B"
elif (camera_number == 23):
camera_name_cctv = "5C"
elif (camera_number == 24):
camera_name_cctv = "5D"
elif (camera_number == 25):
camera_name_cctv = "5P1"
elif (camera_number == 26):
camera_name_cctv = "5P2"
elif (camera_number == 27):
camera_name_cctv = "6A"
elif (camera_number == 28):
camera_name_cctv = "6B"
elif (camera_number == 29):
camera_name_cctv = "6C"
elif (camera_number == 30):
camera_name_cctv = "6D"
elif (camera_number == 31):
camera_name_cctv = "6S1"
elif (camera_number == 32):
camera_name_cctv = "6S2"
camera_name = str(train_number) + "_" + "nvr" + str(car_number) + "_" + camera_name_cctv
item_camera.setAttribute("value", camera_name)
print("camera_name=", camera_name)
print('\n')
# 修改后的树状结构重新写xml文件
xml_file_path = "foo_profiles/car_" + str(train_number) + ".xml"
f= open(xml_file_path, 'w')
dom.writexml(f,indent='',addindent=' ',newl=' ',encoding='UTF-8')
f.close()
alter(xml_file_path,"<carno","\n<carno") # xml文件字符串内容替换
# 调用函数,输入参数列车号
# update_xml_cfg(30)
max_count = 67
loop_count = 30
while loop_count <= max_count:
update_xml_cfg(loop_count)
loop_count += 1
print('###### xml配置文件修改结束 ######\n')
|