zoukankan      html  css  js  c++  java
  • (垃圾代码)修改同目录下面的xml文件标签数值

    import os
    import xml.dom.minidom


    def replace_value(xmlpath, tag_name, n):
    try:
    # 文件读取
    dom = xml.dom.minidom.parse(xmlpath)
    root = dom.documentElement
    # 获取标签对name之间的值
    name = root.getElementsByTagName(tag_name)
    for i in range(len(name)):
    newdata = int(float(name[i].firstChild.data) // n)
    name[i].firstChild.data = str(newdata)
    with open(xmlpath, 'w') as fh:
    dom.writexml(fh)
    print('写入OK')
    return 'ok'
    except Exception as e:
    print(e)
    print('未知错误,请截图联系作者')
    return 'fail'


    def get_all_files():
    mypath = os.getcwd()

    allfiles = os.listdir(mypath)

    return allfiles


    if __name__ == '__main__':
    tag_choose = '1'
    files_list = get_all_files()
    while True:
    file_name = input("请输入您要修改的xml文件名,不要带.xml后缀,只输入名字即可,按回车确认:")
    while file_name + '.xml' not in files_list:
    file_name = input("查询不到您输入的文件名,请确认与本程序在同一目录下后重新输入,按回车确认:")
    files_list = get_all_files()
    while tag_choose == '1':
    tag_name = input("请输入您要修改的标签名,按回车确认:")
    n = float(input("请输入您要整除的倍数,如需放大请除以小数(如放大至5倍则输入0.2, 缩小至一半则输入2):"))
    result = replace_value(os.path.join(os.getcwd(),file_name+'.xml'), tag_name, n)
    if result == 'ok':
    tag_choose = input("写入成功,继续编辑其他标签请输入1,编辑其他文件请输入2,退出本程序请输入n")
    elif result == 'fail':
    tag_choose = input("写入失败,继续编辑其他标签请输入1,编辑其他文件请输入2,退出本程序请输入n")

    if tag_choose == '2':
    break
    if tag_choose == 'n':
    break
  • 相关阅读:
    MySQL数据库的基本数据类型
    Mybatis学习记录(八)----Mybatis整合Spring
    Mybatis学习记录(七)----Mybatis查询缓存
    Mybatis学习记录(七)----Mybatis延迟加载
    Mybatis学习记录(六)----Mybatis的高级映射
    Mybatis学习记录(五)----Mybatis的动态SQL
    Mybatis学习记录(四)----resultMap的使用
    Mybatis学习记录(三)----理解SqlMapConfig.xml文件
    Vue生命周期
    vue简单路由(二)
  • 原文地址:https://www.cnblogs.com/djflask/p/12566139.html
Copyright © 2011-2022 走看看