本文整理汇总了Python中xml.etree.ElementTree.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, xmlfile=None, root=None, adimentionalized=True):
if xmlfile is not None:
if isinstance(xmlfile, file) or isinstance(xmlfile, StringIO.StringIO):
self.tree = parse(xmlfile)
elif isinstance(xmlfile, str):
source = file(xmlfile, "r")
self.tree = parse(source)
source.close()
self.root = self.tree.getroot()
elif root is not None:
self.root = root
else:
print 'A etree root or a xmlfile should be provided'
list = []
a = self.root.findall("param")
for i in a:
n = i.get("name")
v = i.get("value")
if '.' in v or 'e' in v:
v = float(v)
else:
v = int(v)
list.append((n, v))
self.absolute_dic = dict(list)
self.relative_dic = dict(list)
if adimentionalized:
self.adimentionalize()
示例2: lookup_series_info
def lookup_series_info(self, name):
self.find_mirror()
full_url = self.active_mirror + SERIES_LOOKUP_URL % (name.strip().replace(' ', '%20'),)
if self.debug:
print "Getting data for url: %s" % full_url
series_xml = parse(urllib2.urlopen(full_url))
series = [Series for Series in series_xml.findall('Series')]
# now that we have the id's, get the full series info
full_series = []
for s in series:
id = self.get_int(s, 'id', 0)
full_url = self.active_mirror + SERIES_URL % (API_KEY, id)
full_series_xml = parse(urllib2.urlopen(full_url))
for full_s in full_series_xml.findall('Series'):
full_series.append(full_s)
if self.debug:
print " found %i series:" % len(full_series)
to_return = []
for s in full_series:
s_obj = self.parse_series_xml(s)
to_return.append(s_obj)
if self.debug:
print " found series '%s'" % s_obj.title
return to_return
示例3: read_robofab_glyphlist
def read_robofab_glyphlist(libfname, contentsfname) :
etree = parse(libfname)
glist = None
for e in etree.getroot().iterfind('dict') :
d = read_any(e)
if "org.robofab.glyphOrder" in d :
glist = d["org.robofab.glyphOrder"]
if not glist : return
etree = parse(contentsfname)
files = {}
for e in etree.getroot().iterfind("dict") :
d = read_any(e)
f = file(contentsfname, "w")
f.write("""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
""")
for g in glist :
f.write(""" <key>{0}</key>
<string>{1}</string>
""".format(g, d[g]))
f.write("""</dict>
</plist>
""")
示例4: _xml_parse_gz_zip_file
def _xml_parse_gz_zip_file(self, zfile):
"""Recursively check for xml file in zip / gzipped files, and parse it
"""
# Check for zipfile
self.log(logging.DEBUG, "Check ZIP/gz/xml : checking %s " %
(repr(zfile),))
zfile.seek(0)
if zipfile.is_zipfile(zfile):
with ZipFile(zfile) as zc:
nl = zc.namelist()
if len(nl) == 1 and nl[0].endswith(".xml"):
return parse(zc.open(nl[0], 'r'))
else: # Check for gzip file
with gzip.GzipFile(None, 'r', 9, zfile) as zc:
zc.rewind()
with os.tmpfile() as tf:
try:
tf.write(zc.read())
return self._xml_parse_gz_zip_file(tf)
except IOError:
zfile.seek(0)
try:
return parse(zfile)
except ParseError:
return None
示例5: merge_databases
def merge_databases(self, first_xml_db_filename, second_xml_db_filename,
destination_filename):
first_xml_db = parse(first_xml_db_filename).getroot()
second_xml_db = parse(second_xml_db_filename).getroot()
self.add_missing_groups(first_xml_db, second_xml_db)
self.merge_entries(first_xml_db, second_xml_db)
ElementTree(second_xml_db).write(destination_filename)
示例6: getValue
def getValue(key):
if lang == 'en':
data = parse(getScriptLoc() + '/../../src_data/full_text.en.xml').getroot()
elif lang == 'es':
data = parse(getScriptLoc() + '/../../src_data/full_text.es.xml').getroot()
else:
raise Exception('invalid language')
return data.find(key).text
示例7: assert_xml_equal
def assert_xml_equal(a, b):
"""
Compare two XML artifacts for equality.
:param a, b:
Paths to XML files, or a file-like object containing the XML
contents.
"""
tools.assert_equal(tostring(parse(a).getroot()),
tostring(parse(b).getroot()))
示例8: getCraftPricing
def getCraftPricing(rinfo, item):
wowheadxml = None
# Look up mats for item number - check db first
reagents = db.getReagents(item)
if len(reagents) < 1:
# Not there, so check wowhead
wowheadxml = parse(urllib.urlopen(WOWHEAD_BASE % (item)))
ingreds = wowheadxml.findall('item/createdBy/spell/reagent')
reagents = [(x.get('id'), x.get('count')) for x in ingreds]
item_map = [(x.get('id'), x.get('name')) for x in ingreds]
# Store in databases
db.saveReagents(item, reagents)
db.saveItems(item_map)
# Grab item name
item_name = db.getItemName(item)
if not item_name:
# Look it up
if not wowheadxml:
wowheadxml = parse(urllib.urlopen(WOWHEAD_BASE % (item)))
item_name = wowheadxml.find('item/name').text
db.saveItems([(item, item_name)])
# Compare prices, etc.
# Query spreads for item and its reagents
# TODO lookup reagent, but we ignore count for now
spreads = db.getMultiCurrentSpread(rinfo, [item] + [x[0] for x in reagents])
ts, prices = spreads[item]
if ts == -1 or 'buy' not in prices:
return False
crafted = prices['buy'][0]
rprices = []
rnames = []
for id,count in reagents:
rnames.append(db.getItemName(id))
ts, prices = spreads[id]
if ts == None or 'buy' not in prices:
return False
rprices.append(prices['buy'][0])
return {
'item': item_name,
'reagents': rnames,
'profit': crafted - sum(rprices),
'sell': crafted,
'buy': rprices,
}
示例9: __init__
def __init__(self, builder):
if not os.path.isfile(setupXml):
self.initSetup(setupXml)
if not os.path.isfile(currentRegisterXml):
self.initRegister(currentRegisterXml)
self.currentXmlDom = parse(currentRegisterXml)
if not os.path.isfile(previousRegisterXml):
self._fromXmlDom2File(self.currentXmlDom, previousRegisterXml)
self.previousXmlDom = parse(previousRegisterXml)
self.builder = builder
示例10: _assertETXMLWellFormed
def _assertETXMLWellFormed(self, data, parse, msg=None, context=2):
"""internal function used by /assertXML(String)?WellFormed/ functions
:param data: xml_data
:param parse: appropriate parser function for this data
:param msg: error message
:param context: number of context lines in standard message
(show all data if negative).
Only available with element tree
"""
from xml.parsers.expat import ExpatError
try:
from xml.etree.ElementTree import ParseError
except ImportError:
# compatibility for <python2.7
ParseError = ExpatError
try:
parse(data)
except (ExpatError, ParseError) as ex:
if msg is None:
if hasattr(data, "readlines"): # file like object
data.seek(0)
lines = data.readlines()
else:
lines = data.splitlines(True)
nb_lines = len(lines)
context_lines = []
# catch when ParseError doesn't set valid lineno
if ex.lineno is not None:
if context < 0:
start = 1
end = nb_lines
else:
start = max(ex.lineno - context, 1)
end = min(ex.lineno + context, nb_lines)
line_number_length = len("%i" % end)
line_pattern = " %%%ii: %%s" % line_number_length
for line_no in range(start, ex.lineno):
context_lines.append(line_pattern % (line_no, lines[line_no - 1]))
context_lines.append(line_pattern % (ex.lineno, lines[ex.lineno - 1]))
context_lines.append("%s^\n" % (" " * (1 + line_number_length + 2 + ex.offset)))
for line_no in range(ex.lineno + 1, end + 1):
context_lines.append(line_pattern % (line_no, lines[line_no - 1]))
rich_context = "".join(context_lines)
msg = "XML stream not well formed: %s\n%s" % (ex, rich_context)
self.fail(msg)
示例11: assert_xml_equal
def assert_xml_equal(a, b):
"""
Compare two XML artifacts for equality.
:param a, b:
Paths to XML files, or a file-like object containing the XML
contents.
"""
path_a = get_path(a)
path_b = get_path(b)
content_a = tostring(parse(a).getroot())
content_b = tostring(parse(b).getroot())
if content_a != content_b:
raise AssertionError('The files %s and %s are different!' %
(path_a, path_b))
示例12: addToWorkingSet
def addToWorkingSet(newProjectPath):
workingSetFilePath = os.path.expanduser("~") + os.sep + ".colt" + os.sep + "workingset.xml"
projectsList = []
# Populate projects list
if os.path.exists(workingSetFilePath) :
workingSetElement = parse(workingSetFilePath).getroot()
for projectElement in workingSetElement :
projectPath = projectElement.attrib["path"]
if projectPath :
projectsList.append(projectPath)
# Remove project path from the list
projectsList = filter(lambda projectPath : projectPath != newProjectPath, projectsList)
# Push new project
projectsList.insert(0, newProjectPath)
# Save the list
workingSetElement = Element("workingset")
workingSetElement.set("openRecent", "true")
for projectPath in projectsList :
projectElement = SubElement(workingSetElement, "project")
projectElement.set("path", projectPath)
workingSetFile = open(workingSetFilePath, "w")
workingSetFile.write(tostring(workingSetElement))
workingSetFile.close()
示例13: extract_connections_credentials
def extract_connections_credentials(self):
"""
Extract all connection's credentials.
:return: List of dict in which one dict contains all information for a connection.
"""
repos_creds = []
connection_file_location = os.path.join(
constant.profile["USERPROFILE"],
u'.ApacheDirectoryStudio\\.metadata\\.plugins\\org.apache.directory.studio.connection.core\\connections.xml'
)
if os.path.isfile(connection_file_location):
try:
connections = parse(connection_file_location).getroot()
connection_nodes = connections.findall(".//connection")
for connection_node in connection_nodes:
creds = {}
for connection_attr_name in connection_node.attrib:
if connection_attr_name in self.attr_to_extract:
creds[connection_attr_name] = connection_node.attrib[connection_attr_name].strip()
if creds:
repos_creds.append(creds)
except Exception as e:
self.error(u"Cannot retrieve connections credentials '%s'" % e)
return repos_creds
示例14: step_back
def step_back(io_manager):
'''
deletes the output files from the most recent runs
:type io_manager: IOManger
'''
current_iteration = None
target_iteration = None
# Figure out the current iteration and modify the scan specs file appropriately
parameter_scan_specs_xml_file_path = io_manager.parameter_scan_specs_xml_file_path
xml_file = parse(parameter_scan_specs_xml_file_path)
xml_root = xml_file.getroot()
for parameter_element in xml_root.iter('Parameter'):
current_iteration = int(parameter_element.attrib['CurrentIteration'])
target_iteration = current_iteration - 1
print('Stepping up files to redo batch run {}'.format(target_iteration))
parameter_element.set('CurrentIteration', str(target_iteration))
ElementTree(xml_root).write(parameter_scan_specs_xml_file_path)
# Remove screenshots
for root, dirs, files in os.walk(io_manager.screenshot_output_path):
for dir in dirs:
if dir == str(target_iteration):
shutil.rmtree(os.path.join(root, dir))
# Remove most recent .csv and .txt in output folder
for root, dirs, files in os.walk(io_manager.output_folder):
for file in files:
if fnmatch(file, '*output{}.txt'.format(target_iteration)) or fnmatch(file, '*output{}.csv'.format(target_iteration)):
os.remove(os.path.join(root, file))
示例15: modify_vrt
def modify_vrt(vrt, scale):
"""
Makes modifications to the vrt file to fix the values.
:param vrt: VRT file to be processed
:param scale: Scale value from get_metadata_item function
:return: None
"""
doc = parse(vrt)
root = doc.getroot()
# Fix the datatype if it is wrong
raster_band = root.find('VRTRasterBand')
raster_band.set('dataType', 'Float32')
# Add the scale to the vrt file
source = root.find('VRTRasterBand').find('ComplexSource')
scale_ratio = SubElement(source, 'ScaleRatio')
scale_ratio.text = scale
# Write the scale input
# vrt files are overwritten with the same name
doc.write(vrt, xml_declaration=True)