我需要在python中使用etree读取XML文件,我有以下代码

时间:2015-04-13 06:07:49

标签: python

如果xml文件中存在XMLNS标记,则代码不起作用。如何删除XMLNS标记,以便我可以读取xml并更新xml中影响XML格式的值。如果我不删除XMLNS标记,则会通过添加名称空间来破坏我的xml文件。

import os
import re
import sys
import math
import shutil
import string
import subprocess
from xml.dom import minidom
from xml.etree import ElementTree as et
from optparse import OptionParser
from array import *

with open("file1.txt") as input_file:
    for line in input_file:
        line = line.strip()
        for number in line.split():
            #int(data[0])
            data=(float(number))

data=int(math.ceil(data))
data=(data*1024*1024)
data=hex(data)[2:]
tree = et.parse("..\FFUPackages\MemoryReservation.pkg.xml")
root = tree.getroot()

#del root.attrib["xmlns"]
#root[0][0][0][0].set("text","tony")
labels = tree.findall("Components/OSComponent/RegKeys/RegKey/RegValue")
#print labels
for label in labels:
    #lineNum = lineNum + 1
    #print label.find("Name")
    if (label.get("Name")=="MemoryReservation"):
        print label.get("Name")
        #print label.set("int", data)
        #self.logger.info("\t>Updating QCLabel.xml")
        label.set("Value", str(data))
root.set("xmlns","urn:Microsoft.WindowsPhone/PackageSchema.v8.00")

tree.write("..\FFUPackages\MemoryReservation.pkg.xml")

0 个答案:

没有答案