本文整理匯總了Python中elementtree.ElementTree.ElementTree.getiterator方法的典型用法代碼示例。如果您正苦於以下問題:Python ElementTree.getiterator方法的具體用法?Python ElementTree.getiterator怎麽用?Python ElementTree.getiterator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類elementtree.ElementTree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.getiterator方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: scan_kid_files
# 需要導入模塊: from elementtree.ElementTree import ElementTree [as 別名]
# 或者: from elementtree.ElementTree.ElementTree import getiterator [as 別名]
def scan_kid_files(self, potfile, files):
messages = []
tags_to_ignore = ['script', 'style']
keys = []
kid_expr_re = re.compile(r"_\(('(?P<texta>[^']*)'|\"(?P<textb>[^\"]*)\")\)")
for fname in files:
print 'working on', fname
tree = None
try:
tree = ElementTree(file=fname).getroot()
except Exception, e:
print 'Skip %s: %s' % (fname, e)
continue
for el in tree.getiterator():
if self.options.loose_kid_support or el.get('lang', None):
tag = re.sub('({[^}]+})?(\w+)', '\\2', el.tag)
ents = []
if el.text: ents = [el.text.strip()]
if el.attrib: ents.extend(el.attrib.values())
for k in ents:
key = None
s = kid_expr_re.search(k)
if s:
key = s.groupdict()['texta'] or s.groupdict()['textb']
if key and (key not in keys) and (tag not in tags_to_ignore):
messages.append((tag, fname, key))
keys.append(key)
示例2: lpcParameterParser
# 需要導入模塊: from elementtree.ElementTree import ElementTree [as 別名]
# 或者: from elementtree.ElementTree.ElementTree import getiterator [as 別名]
class lpcParameterParser(object):
'''
Returns dictionary with keys 'type' and 'params'
'''
def __init__(self, filename):
'''
Parameters
----------
filename : path to xml config file (TODO - SCHEMA definition)
'''
self._filename = filename
self._config_tree = ElementTree(file = filename)
def _assignDictionaryItem(self, item_dict):
s = 'v=' + item_dict['type'] + '("' + item_dict['value'] + '")'
exec(s) #TODO - remove exec, there are clearly better ways to do this!
return v
def _generateParamDictionary(self, tag):
'''Generates a dictionary containing 'type', a string defining the 'type' attribute of element 'tag'
and 'params', a dictionary of parameters to be unpacked as arguments to constructors for instances of 'type'
'''
parser_type_tag = self.__class__.TYPE_TAG
parser_type_node = self._config_tree.getiterator(parser_type_tag)
if len(parser_type_node) == 1:
param_node = parser_type_node[0].getiterator(tag)
if len(param_node) == 1:
param_type = param_node[0].get('type')
params = {}
for par in param_node[0]:
items = dict(par.items())
if items['type'] == 'list':
elts = par.getiterator('elt')
temp_list = []
for e in elts:
list_attr = dict(e.items())
temp_list.append(self._assignDictionaryItem(list_attr))
params[items['name']] = temp_list
else:
params[items['name']] = self._assignDictionaryItem(items)
return {'type': param_type, 'params': params}
else:
msg = 'The required unique lpc configuration element tag, ' + parser_type_tag + ' is missing or not unique from ' + self._filename
raise ValueError, msg
else:
msg = 'The required unique lpc configuration element tag, ' + parser_type_tag + ' is missing or not unique from ' + self._filename
raise ValueError, msg
示例3: ElementTree
# 需要導入模塊: from elementtree.ElementTree import ElementTree [as 別名]
# 或者: from elementtree.ElementTree.ElementTree import getiterator [as 別名]
#!/usr/bin/env python
# Bugs
#
# - mehrfach vorkommendes wird vom aktuellsten ueberschrieben
# - attribute + text geht nicht gleichzeitig
# - 3-dimensional geht nicht
import sys, pprint
from elementtree.ElementTree import ElementTree
root = ElementTree(file='dirt.xml')
iter = root.getiterator()
config = {}
lasttag = []
lastvalue = []
for element in iter:
#if element.keys():
# tempdict = {}
# for name, value in element.items():
# tempdict[name] = value
# config[element.tag] = tempdict
#if element.text and element.text.strip() != "":
# text = element.text
# config[element.tag] = element.text
if element.getchildren():
if not element.keys():
config[element.tag] = "NOCHWAS"
for child in element:
if config.has_key(child.tag):