使用lxml库非常容易实现
from lxml import objectify
class Parser(object):
def __init__(self, tree, counter_start, counter_interval):
self.tree = tree
self.root = tree.getroot()
self.counter_start = counter_start
self.counter_interval = counter_interval
def parse(self):
counter = self.counter_start
# for loop to iter voltage items
# using counter += counter_interval to set the value for example
# save the tree within the parser class or in the handle function
def handle(file):
f = open(file)
tree = objectify.parse(f)
parser = Parser(tree, 1000, 100)
parser.parse()
f.close()
handle("/Desktop/bar.XML")