自己写的一个从Xml中提取指定内容,调整之后更新至源文件的脚本,记录一下:
#!/usr/bin/python
import sys
import os,os.path
import argparse
import xml.etree.ElementTree as ET
def anXml(fileName,testXml):
a = ET.Element("root")
sumCount=0
xmlFp = ET.parse(fileName)
root=xmlFp.getroot()
for child in root:
content=child.get('content')
if content is not None:
num=content.count('%s')+content.count('%d')
if num>=2:
sumCount+=1
print(child.attrib['id'])
newNode = ET.SubElement(a,"info")
for eva in child.attrib:
newNode.attrib[eva] = child.attrib[eva]
print('Sum Count:'+ str(sumCount))
tree = ET.ElementTree(a)
tree.write(testXml,encoding = 'utf-8')
def updateXml(fileName,testXml):
tmpDic={};
xmlFp = ET.parse(testXml)
root=xmlFp.getroot()
for child in root:
id=child.get('id')
content=child.get('content')
tmpDic[id]=content
xmlFp = ET.parse(fileName)
root=xmlFp.getroot()
for node in root.findall("language"):
content=tmpDic.get(node.attrib['id'])
if content is not None:
print(content)
if content=='':
root.remove(node)
else:
node.attrib['content']=content
xmlFp.write(fileName,encoding = 'utf-8')
def init(path,mode):
print(path)
languageXml=path+'Language.xml'
testXml=path+'test.xml'
if mode is None or mode==0:
anXml(languageXml,testXml)
else:
updateXml(languageXml,testXml)
if __name__=='__main__':
parser = argparse.ArgumentParser(description='Process rename floders root path.')
parser.add_argument('-p', '--path', help='floder path')
parser.add_argument('-m', '--mode', help='handle type')
args = parser.parse_args()
init(args.path+'\\',args.mode)