本文整理汇总了Python中xml.etree.ElementTree.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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()
开发者ID:glyg,项目名称:Kinetochore-segregation,代码行数:32,代码来源:xml_handler.py
示例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
开发者ID:euphoria,项目名称:meliman,代码行数:33,代码来源:thetvdb.py
示例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>
""")
开发者ID:HughP,项目名称:Palaso-Python,代码行数:26,代码来源:ufo.py
示例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
开发者ID:jmbarbier,项目名称:djeskoolz,代码行数:25,代码来源:common.py
示例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)
开发者ID:sirswa,项目名称:scripts,代码行数:8,代码来源:merge-keepass.py
示例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
开发者ID:USGS-CIDA,项目名称:OWDI-Lower-Colorado-Drought-Vis,代码行数:8,代码来源:createFlowHistoryGraph.py
示例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()))
开发者ID:MohsenKohrangi,项目名称:oq-nrmllib,代码行数:10,代码来源:_utils.py
示例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,
}
开发者ID:typpo,项目名称:wowzer,代码行数:55,代码来源:arbitrage.py
示例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
开发者ID:arulalant,项目名称:mmDiagnosis,代码行数:11,代码来源:xmltree.py
示例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)
开发者ID:snelson-senet,项目名称:linux_config,代码行数:50,代码来源:testlib.py
示例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))
开发者ID:ruthali,项目名称:oq-risklib,代码行数:15,代码来源:_utils.py
示例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()
开发者ID:Shchvova,项目名称:colt-sublime-plugin,代码行数:29,代码来源:colt.py
示例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
开发者ID:cclauss,项目名称:LaZagne,代码行数:26,代码来源:apachedirectorystudio.py
示例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))
开发者ID:ram8647,项目名称:tcseg,代码行数:32,代码来源:StepBackBatch.py
示例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)
开发者ID:OpenGeoscience,项目名称:nex,代码行数:25,代码来源:hdf2tif.py
示例16: _load_from_file
def _load_from_file(self, filename):
"""Builds a CTDModel from a CTD XML file.
"""
root = parse(filename).getroot()
assert root.tag == 'tool', "Invalid CTD file, root is not <tool>" # TODO: own exception
self.opt_attribs = {}
for tool_required_attrib in ['name', 'version']:
assert tool_required_attrib in root.attrib, "CTD tool is missing a %s attribute" % tool_required_attrib
setattr(self, tool_required_attrib, root.attrib[tool_required_attrib])
for tool_opt_attrib in ['docurl', 'category']:
if tool_opt_attrib in root.attrib:
self.opt_attribs[tool_opt_attrib] = root.attrib[tool_opt_attrib]
for tool_element in root:
if tool_element.tag in ['manual', 'description', 'executableName', 'executablePath']:
# ignoring: cli, logs, relocators. cli and relocators might be useful later.
self.opt_attribs[tool_element.tag] = tool_element.text
if tool_element.tag == 'PARAMETERS':
# tool_element.attrib['version'] == '1.6.2' # check whether the schema matches the one CTDOpts uses?
params_container_node = tool_element.find('NODE')
# we have to check the case in which the parent node contains
# item/itemlist elements AND node element children
params_container_node_contains_items = params_container_node.find('ITEM') is not None or params_container_node.find('ITEMLIST')
# assert params_container_node.attrib['name'] == self.name
# check params_container_node's first ITEM child's tool version information again? (OpenMS legacy?)
params = params_container_node.find('NODE') # OpenMS legacy again, NODE with name="1" on top
# check for the case when we have PARAMETERS/NODE/ITEM
if params is None or params_container_node_contains_items:
self.parameters = self._build_param_model(params_container_node, base=None)
else:
# OpenMS legacy again, PARAMETERS/NODE/NODE/ITEM
self.parameters = self._build_param_model(params, base=None)
开发者ID:b-schubert,项目名称:CTDopts,代码行数:35,代码来源:CTDopts.py
示例17: getReportparameters
def getReportparameters(filename):
parms = {}
csidParms = True
fileFound = False
try:
reportXML = parse(JRXMLDIRPATTERN % filename)
fileFound = True
parameters = reportXML.findall('{http://jasperreports.sourceforge.net/jasperreports}parameter')
#print 'parameters',parameters
for p in parameters:
name = p.attrib['name']
isForPrompting = p.get('isForPrompting')
if name == 'csid':
csidParms = False
try:
default = p.find('{http://jasperreports.sourceforge.net/jasperreports}defaultValueExpression').text
except:
default = ''
try:
description = p.find('{http://jasperreports.sourceforge.net/jasperreports}parameterDescription').text
except:
description = ''
parms[name] = [default.strip('"'), isForPrompting, description.strip('"')]
except:
#raise
# indicate that .jrxml file was not found...
print 'jrxml file not found, no parms extracted.'
return parms,csidParms,fileFound
开发者ID:RjLi13,项目名称:pahma_project,代码行数:28,代码来源:views.py
示例18: adr2geo
def adr2geo(adr):
api = "http://www.geocoding.jp/api/?v=1.1&q=%s" % (urllib.quote(adr.encode('utf-8')))
xml = parse(urllib.urlopen(api)).getroot()
lat = xml.find('coordinate/lat').text
lng = xml.find('coordinate/lng').text
return (float(lat), float(lng))
开发者ID:toshi123,项目名称:adr2distance.py,代码行数:7,代码来源:adr2distance.py
示例19: read_xml_config_old
def read_xml_config_old(self, configfile):
"""
Reads the XML configuration file used in data taking
and reads the configuration object. Reads the configuration
tag and reads the text in there and assigns actual receiver
elements to rows and columns from binary file
This one is for the old 3 column configuration we used to have
"""
if not os.path.exists(configfile):
raise Exception("Config file %s does not exist" % configfile)
tree = parse(configfile)
elem = tree.getroot()
cfg = elem.find("configuration")
txt = cfg.text
rows = txt.split('\n\t')
self.pixeldic = {} # for a given frontend pixel has
# tuple of row, col of bf
for row in rows[2:-1]:
args = row.split('\t')
adc = int(args[1])
cable = int(args[2])
pixel = args[3].strip()
print "ADC: %d, cable: %d, pixel: %s" % (adc, cable, pixel)
if pixel != 'NC':
self.pixeldic[pixel] = get_rowcol_for_cable(cable)
self.pixel_label = dict((v, k) for k, v in self.pixeldic.iteritems())
开发者ID:gopastro,项目名称:pyphamas,代码行数:26,代码来源:binfile_parser.py
示例20: get_project_files
def get_project_files(filename, filetype=None, minfiles=0):
"""Parse Xilinx .xise file and extract source files having type
<filetype>. No path normalization is done here."""
tree = parse(filename)
root = tree.getroot()
files = root.find('{http://www.xilinx.com/XMLSchema}files')
properties = root.find('{http://www.xilinx.com/XMLSchema}properties')
# Find chipscope files
if filetype is None:
matchFun = lambda ft: True
else:
matchFun = lambda ft: (ft == filetype)
matchingFiles = [f.get('{http://www.xilinx.com/XMLSchema}name')
for f in files.findall('{http://www.xilinx.com/XMLSchema}file')
if matchFun(f.attrib['{http://www.xilinx.com/XMLSchema}type'])]
if len(matchingFiles) < minfiles:
msg = "Required at least {0:d} files of type {1}, found only {2:d}: {3} ".format(minfiles, filetype, len(matchingFiles), matchingFiles)
raise XiseMissingFilesError(msg)
return matchingFiles
开发者ID:enchanter,项目名称:xilinx-build-scripts,代码行数:26,代码来源:xil_ise.py
注:本文中的xml.etree.ElementTree.parse函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论