python處理xml文件的方法小結(jié)
本文實(shí)例講述了python處理xml文件的方法。分享給大家供大家參考,具體如下:
前一段時(shí)間因?yàn)楣ぷ鞯男枰瑢W(xué)習(xí)了一點(diǎn)用Python處理xml文件的方法,現(xiàn)在貼出來,供大家參考。
xml文件是按節(jié)點(diǎn)一層一層來疊加的,最頂層的是根節(jié)點(diǎn)。比如說:
<sys:String x:Key="STR_License_WithoutLicense">Sorry, you are not authorized.</sys:String>
其中sys:String為節(jié)點(diǎn)名字,x:Key的內(nèi)容為Attribute,xml節(jié)點(diǎn)值為sys:String的子節(jié)點(diǎn),它是文本節(jié)點(diǎn)類型。<節(jié)點(diǎn)名稱 x:Key="Attribute">子節(jié)點(diǎn)。。。
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中取出節(jié)點(diǎn)值,在CMAD的String中查找是否已經(jīng)存在,如果存在,則返回CMAD中對(duì)應(yīng)String的NodeName(節(jié)點(diǎn)名),并把兩個(gè)節(jié)點(diǎn)名一個(gè)做節(jié)點(diǎn)名,一個(gè)做節(jié)點(diǎn)值寫到xml文件中;如果不存在,則把RPD中的該節(jié)點(diǎn)寫到另外一個(gè)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)
#得到根節(jié)點(diǎn)
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))
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作xml數(shù)據(jù)技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python人人網(wǎng)登錄應(yīng)用實(shí)例
這篇文章主要介紹了python人人網(wǎng)登錄應(yīng)用實(shí)例,是一個(gè)非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09
Python爬蟲基礎(chǔ)之XPath語法與lxml庫(kù)的用法詳解
這篇文章主要給大家介紹了關(guān)于Python爬蟲基礎(chǔ)之XPath語法與lxml庫(kù)用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
Python迅速掌握語音識(shí)別之知識(shí)儲(chǔ)備篇
語音識(shí)別是一門交叉學(xué)科。近二十年來,語音識(shí)別技術(shù)取得顯著進(jìn)步,開始從實(shí)驗(yàn)室走向市場(chǎng)。人們預(yù)計(jì),未來10年內(nèi),語音識(shí)別技術(shù)將進(jìn)入工業(yè)、家電、通信、汽車電子、醫(yī)療、家庭服務(wù)、消費(fèi)電子產(chǎn)品等各個(gè)領(lǐng)域2021-11-11
python使用Streamlit庫(kù)制作Web可視化頁(yè)面
一談到Web頁(yè)面,可能大家首先想到就是HTML,CSS或JavaScript。 本次小F就給大家介紹一下如何用Python制作一個(gè)數(shù)據(jù)可視化網(wǎng)頁(yè),使用到的是Streamlit庫(kù)。輕松的將一個(gè)Excel數(shù)據(jù)文件轉(zhuǎn)換為一個(gè)Web頁(yè)面,提供給所有人在線查看。2021-05-05
pytorch 一行代碼查看網(wǎng)絡(luò)參數(shù)總量的實(shí)現(xiàn)
這篇文章主要介紹了pytorch實(shí)現(xiàn)一行代碼查看網(wǎng)絡(luò)參數(shù)總量的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-05-05

