IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> python批量修改xml配置文件 -> 正文阅读

[Python知识库]python批量修改xml配置文件

# -*- 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')

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-11-10 12:21:06  更:2021-11-10 12:22:48 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 22:32:50-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码