笔记:写python脚本去遍历XML文件并修改某个属性(九宫格批量修改)【总】
2019-03-07 09:16
429 查看
python遍历XML文件并修改某个属性(九宫格批量修改)
因为是第一次使用python写脚本工具,很多不知的知识,本人记下代码,恳请各位指正:
用的是python 2.7,修改的是白鹭引擎下.exml(UI文件):
ps:代码有点长,我会在本博客其他文章一 一解释我对各个函数的理解。
import os import xml.dom.minidom #设定一组指定的文件名扩展,用list方便添加其他类型文件 file_format_list = [".exml"] #存放满足要求的exml文件 exml_file_ls = [] #通过图片名 全文件夹搜索出有用这个图片的exml文件并查找exml文件中的九宫格值,函数直接返回九宫格值 def Search_by_picname(searchpath, picnamestr): find_emxl_file(searchpath, exml_file_ls) s9g_Value_list = [] for exml_file in exml_file_ls: Search_s9Grid_in_exmlfile(exml_file, picnamestr, s9g_Value_list) return s9g_Value_list #判断是否是exml文件 def is_exml_file(full_path, file_name): flag_exml=False for extend in file_format_list: if extend in file_name: flag_exml = True return flag_exml #查找.emxl文件,存在就放进exml_file_ls列表中 def find_emxl_file(searchpath, exml_file_ls): if not os.path.exists(searchpath): return None for file_name in os.listdir(searchpath): full_path = os.path.join(searchpath,file_name) if os.path.isfile(full_path): if is_exml_file(full_path, file_name): exml_file_ls.append(full_path) elif os.path.isdir(full_path): find_emxl_file(full_path, exml_file_ls) #在exml文件中查找九宫格字段,有就存进s9g_Value_list def Search_s9Grid_in_exmlfile(exml_file, picnamestr, s9g_Value_list): dom = xml.dom.minidom.parse(exml_file) #得到文档元素对象 root = dom.documentElement itemlist = root.getElementsByTagName("e:Image") if len(itemlist)<=0: return for item in itemlist: picnameValue = item.getAttribute("source") picnameValue_u = picnameValue.encode("utf-8") if picnameValue_u == picnamestr: s9gValue = item.getAttribute("scale9Grid") #xml九宫格的键值 s9g_Value_list.append(s9gValue) #函数Show_s9gValue_Type:按相同九宫格方式为一组,分别输出到屏幕上 def Show_s9gValue_group(s9g_Value_goup, isUse): index = 0 for value in s9g_Value_goup: if len(value)>0: isUse = True else: isUse = False print ("index="+str(index)+" \t"+str(isUse)+" "+"\tnow_s9Grid: "+"\t"+value) index += 1 #返回选择的那个需要改变的九宫格值 def Select_Change_by_idx(s9g_Value_goup,idx): idx = int(idx) glist = list(s9g_Value_goup) gvalue = glist[idx] print("before needChange_s9gvalue: "+gvalue) return gvalue #修改后保存一下xml文件 def save_xml_file(file_name, dom): with open(file_name, 'w') as f: f.write(dom.toprettyxml(indent="",newl="",encoding='utf-8')) f.close() #修改九宫格值 def Change_s9Grid_Value(needChange_s9gvalue,new_value,picnamestr): for exml_file in exml_file_ls: Xml_SetAttribute(exml_file,needChange_s9gvalue,new_value,picnamestr) #进入到具体xml文件搜索九宫格值并修改 def Xml_SetAttribute(exml_file,needChange_s9gvalue,new_value,picnamestr): dom = xml.dom.minidom.parse(exml_file) #得到文档元素对象 root = dom.documentElement itemlist = root.getElementsByTagName("e:Image") if len(itemlist)<=0: return isChange = False for item in itemlist: picnameValue = item.getAttribute("source") picnameValue_u = picnameValue.encode("utf-8") if picnameValue_u == picnamestr: s9gValue = item.getAttribute("scale9Grid") #xml九宫格的键值 if needChange_s9gvalue == s9gValue: item.setAttribute("scale9Grid",new_value) isChange = True if isChange: save_xml_file(exml_file, dom) print("Change Success! "+exml_file) #进入到具体xml文件搜索九宫格值并移除九宫格 def Xml_RemoveAttribute(exml_file,needChange_s9gvalue,picnamestr): dom = xml.dom.minidom.parse(exml_file) #得到文档元素对象 root = dom.documentElement itemlist = root.getElementsByTagName("e:Image") if len(itemlist)<=0: return isDelete = False for item in itemlist: picnameValue = item.getAttribute("source") picnameValue_u = picnameValue.encode("utf-8") if picnameValue_u == picnamestr: s9gValue = item.getAttribute("scale9Grid") #xml九宫格的键值 if needChange_s9gvalue == s9gValue: item.removeAttribute("scale9Grid") isDelete = True if isDelete: save_xml_file(exml_file, dom) print("Delete Success! "+exml_file) #删除九宫格值 def Delete_s9Grid_Value(needChange_s9gvalue,picnamestr): for exml_file in exml_file_ls: Xml_RemoveAttribute(exml_file,needChange_s9gvalue,picnamestr) #选择改或删 def Change_and_Delete(input_is_Use,needChange_s9gvalue,new_value,picnamestr): if input_is_Use == "Y": Change_s9Grid_Value(needChange_s9gvalue,new_value,picnamestr) elif input_is_Use == "N": Delete_s9Grid_Value(needChange_s9gvalue,picnamestr) else: print ("please input Y or N") return #输入想修改的索引 def Input_Indexs(): indexs = [] while 1: n = raw_input() if n != "-1": indexs.append(n) else: break return indexs ########## main ↓ ########## def main(): #第一步 picnamestr = raw_input("please input picture name:") searchpath = os.getcwd() print("search path=>"+searchpath+"...") #第二步 s9g_Value_list = Search_by_picname(searchpath, picnamestr) s9g_Value_goup = set(s9g_Value_list) if not s9g_Value_list or len(s9g_Value_list) <= 0: print ('not found') return #第三步 isUse = False Show_s9gValue_group(s9g_Value_goup, isUse) print("\nwould change indexs: ") #第四步 new_value = "" indexs=Input_Indexs() input_is_Use = raw_input("\nis use s9Grid (Y or N): ") #第五步 if input_is_Use == "Y": new_value = raw_input("\nnew s9Grid_value: ") #最后执行 for idx in indexs: needChange_s9gvalue=Select_Change_by_idx(s9g_Value_goup,idx) Change_and_Delete(input_is_Use,needChange_s9gvalue,new_value,picnamestr) #运行main函数 if __name__ == "__main__": main() #此脚本工具是需要放在当前目录下使用的,注意我是修改白鹭引擎的xeml文件,所以我规定了".xeml"格式。若是有需求,可以修改规定格式,操作不同格式的文件
工具展示:
相关文章推荐
- 笔记:写python脚本(九宫格批量修改)之遍历搜索文件【解释一步】
- 笔记:写python脚本(九宫格批量修改)之执行修改,保存修改【解释三步 end】
- 批量修改AD用户属性python脚本
- [原创]通过编写PowerDesigner脚本功能批量修改属性
- Python脚本- 递归遍历文件夹,获取指定扩展名文件,修改文件内容
- python批量修改xml文件的path和filename
- Shell脚本遍历目录并批量修改文件编码
- python脚本批量修改华为交换机端口配置
- 15. Python脚本学习笔记十五特殊方法、属性和迭代器
- Python脚本01 —— 批量修改png图片文件名,增加@3x后缀
- PowerDesigner脚本:批量修改OOM中类的属性及方法名称(20050921)
- 通过编写PowerDesigner脚本功能批量修改属性
- 在ArcGIS中,用Python脚本批量修改FeatureClass的名称与字段
- Python3 写的远程批量修改文件内容的脚本
- 数据库操作-表合并-Python连接Mysql-批量修改列属性-csv读入Python
- Shell脚本遍历目录并批量修改文件编码
- python批量修改xml属性
- 批量修改cvs下面所有Root文件的python脚本
- 一个用Python写的 CSDN 批量文章分类修改脚本
- 多线程学习——python脚本批量修改华为交换机端口配置