这里有新鲜出炉的 Python 入门,程序狗速度看过来!
Python 是一种面向对象、解释型计算机程序设计语言,由 Guido van Rossum 于 1989 年底发明,第一个公开发行版发行于 1991 年。Python 语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是 C/C++)很轻松地联结在一起。
这篇文章主要介绍了 python 处理 xml 文件的方法, 结合实例形式总结分析了 Python 常见的 xml 文件处理技巧与相关注意事项, 需要的朋友可以参考下
本文实例讲述了 python 处理 xml 文件的方法。分享给大家供大家参考,具体如下:
前一段时间因为工作的需要,学习了一点用 Python 处理 xml 文件的方法,现在贴出来,供大家参考。
xml 文件是按节点一层一层来叠加的,最顶层的是根节点。比如说:
- <sys:String x:Key="STR_License_WithoutLicense">
- Sorry, you are not authorized.
- </sys:String>
其中 sys:String 为节点名字,x:Key 的内容为 Attribute,xml 节点值为 sys:String 的子节点,它是文本节点类型。<节点名称 x:Key="Attribute"> 子节点。。。
RPD 的 xml 格式:
- <ResourceDictionary>
- <sys:String x:Key="STR_Startup_LaunchRPD">
- Launching Polycom RealPresence Desktop
- </sys:String>
- <sys:String x:Key="STR_Startup_CheckFolder">
- Checking folder
- </sys:String>
CMAD 的 xml 格式:
- <language-strings>
- <ABK_CALL comment="verb (command, button on screen to press to place a call);"
- controls="Button" products="HDX,VSX,CMAD,Venus Main">
- <ARABIC notes="" last-change-date="" status="">
- 打电话
- </ARABIC>
- <CHINESE_S notes="" last-change-date="" status="">
- 呼叫
- </CHINESE_S>
该代码的功能是:
从 RPD 的 String 中取出节点值,在 CMAD 的 String 中查找是否已经存在,如果存在,则返回 CMAD 中对应 String 的 NodeName(节点名),并把两个节点名一个做节点名,一个做节点值写到 xml 文件中;如果不存在,则把 RPD 中的该节点写到另外一个 xml 文件中。代码如下:
- import xml.dom.minidom from xml.dom.minidom import Document RPD_Str_path = "E:/PythonCode/StringResource.en-US.xaml"RPD_dom = xml.dom.minidom.parse(RPD_Str_path) CMAD_Str_path = "E:/PythonCode/M500_RPM13_0522.xml"CMAD_dom = xml.dom.minidom.parse(CMAD_Str_path)#得到根节点RPD_root = RPD_dom.documentElement CMAD_root = CMAD_dom.documentElement def IsStr_already_Translated(RPD_Str) : for firstLevel in CMAD_root.childNodes: for SecondLevel in firstLevel.childNodes: if SecondLevel.nodeType == SecondLevel.ELEMENT_NODE: if SecondLevel.nodeName == "ENGLISH_US": if RPD_Str == SecondLevel.childNodes[0].data.strip() : return firstLevel.nodeName
- else: continue
- else: continue
- else: continue
- else: continue
- else: return "Null"#用Document来写xml文件Mapping_doc = Document() Mapping_root = Mapping_doc.createElement("Common_String") Mapping_doc.appendChild(Mapping_root) Translation_doc = Document() Translation_root = Translation_doc.createElement("Need_Translation_String") Translation_doc.appendChild(Translation_root) for node in RPD_root.childNodes: if node.nodeType == node.ELEMENT_NODE: #print node.getAttribute("x:Key") + " + " + node.childNodes[0].data CMAD_Key = IsStr_already_Translated(node.childNodes[0].data.strip()) if (CMAD_Key != "Null") : mKey = Mapping_doc.createElement(node.getAttribute("x:Key")) Mapping_root.appendChild(mKey) mValue = Mapping_doc.createTextNode(CMAD_Key) mKey.appendChild(mValue) elif(CMAD_Key == "Null") : Key = Translation_doc.createElement('sys:String') Translation_root.appendChild(Key) Key.setAttribute('x:Key', node.getAttribute("x:Key")) Value = Translation_doc.createTextNode(node.childNodes[0].nodeValue) Key.appendChild(Value) continue
- else: path1 = "E:/PythonCode/ID_Mapping.xml"
- try: import codecs f1 = codecs.open(path1, "wb", "utf-8") f1.write(Mapping_doc.toprettyxml(indent = " ")) except: print('Write xml file failed.... file:{0}'.format(path1)) path2 = "E:/PythonCode/Need_Translate_String.xml"
- try: f2 = codecs.open(path2, "wb", "utf-8") f2.write(Translation_doc.toprettyxml(indent = " ")) except: print('Write xml file failed.... file:{0}'.format(path2))
希望本文所述对大家 Python 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0711/334841.html