本文整理汇总了Python中etree.ElementTree.parse方法的典型用法代码示例。如果您正苦于以下问题:Python ElementTree.parse方法的具体用法?Python ElementTree.parse怎么用?Python ElementTree.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类etree.ElementTree
的用法示例。
在下文中一共展示了ElementTree.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
with open(path, "r") as f:
root = ElementTree.parse(f).getroot()
list = {
"items": [],
}
for i, itemTree in enumerate(root.findall("Vokabeldatensatz/Datensatz")):
word = {
"id": i,
"questions": [],
"answers": [],
}
comments = []
for question in itemTree.findall("Vokabeln/string"):
word["questions"].append((question.text,))
for answer in itemTree.findall("Vokabeln/string"):
word["answers"].append((answer.text,))
for comment in itemTree.findall("Kommentare/string"):
comments.append((comment.text))
if comments:
word["comment"] = u"; ".join(comments)
list["items"].append(word)
return {
"resources": {},
"list": list,
}
示例2: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
wsp = self._modules.default("active", type="wordsStringParser")
with open(path) as f:
root = ElementTree.parse(f).getroot()
wordList = {
"items": [],
}
wordList["questionLanguage"] = root.findtext("fields/field[1]") or u""
wordList["answerLanguage"] = root.findtext("fields/field[2]") or u""
#counter is used as word id
counter = itertools.count()
for entryTree in root.findall("entries/e"):
wordList["items"].append({
"id": next(counter),
"questions": wsp.parse(entryTree.findtext("f[1]") or u""),
"answers": wsp.parse(entryTree.findtext("f[2]") or u""),
"comment": entryTree.findtext("f[3]") or u"",
})
return {
"resources": {},
"list": wordList,
}
示例3: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
"""Loads .fq (FlashQard) files. Based on file format inspection.
(even though source code is available, see
http://flashqard-project.org/download.php)
"""
with open(path, "r") as f:
root = ElementTree.parse(f).getroot()
list = {
"items": [],
"title": root.find("box").get("name"),
}
for i, itemTree in enumerate(root.findall(".//card")):
questions = self._stripTags(itemTree.findtext("frontsidedocument/html"))
answers = self._stripTags(itemTree.findtext("backsidedocument/html"))
created = itemTree.findtext("statistics/dateCreated")
list["items"].append({
"id": i,
"questions": self._parse(questions),
"answers": self._parse(answers),
"comment": self._stripTags(itemTree.findtext("comments")),
"created": datetime.datetime.strptime(created, "%d.%m.%Y")
})
return {
"resources": {},
"list": list,
}
示例4: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
"""Loads Teachmaster's .vok2 files. Lesson info is stored in
additional files, but that's not loaded. It might not map to
the OT result system either (not sure). For documentation on
the file format in German (translations are available, but
those aren't all complete/up-to-date):
http://www.teachmaster.de/wikka/DocumentationDEDateiformate
"""
with open(path, "r") as f:
root = ElementTree.parse(f).getroot()
list = {
"items": [],
"title": root.findtext("header/titel") or u"",
"questionLanguage": root.findtext("header/spreins") or u"",
"answerLanguage": root.findtext("header/sprzwei"),
}
for i, itemTree in enumerate(root.findall("vokabelsatz")):
word = self._loadWordFromItemTree(itemTree)
word["id"] = i
list["items"].append(word)
return {
"resources": {},
"list": list,
}
示例5: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
wsp = self._modules.default("active", type="wordsStringParser")
root = ElementTree.parse(open(path)).getroot()
wordList = {
"items": list(),
"title": root.get("title", u""),
}
#a counter is used to suppply the ids. That's because ABBYY
#Lingvo Tutor decided to not include ids anymore from version X5
#on.
counter = itertools.count()
wordList["items"] = [
{
"id": next(counter),
"questions": wsp.parse(wordTree.findtext("word") or u""),
"answers": [[a.text or u"" for a in wordTree.findall("meanings/meaning/translations/word")]],
"commentAfterAnswering": u", ".join(
e.text or u""
for e in wordTree.findall("meanings/meaning/examples/example")
)
}
for wordTree in root.findall("card")
]
return {
"resources": {},
"list": wordList,
}
示例6: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
#reference implementation:
#http://jmemorize.svn.sourceforge.net/viewvc/jmemorize/trunk/jmemorize/src/jmemorize/core/io/XmlBuilder.java?view=markup
try:
with contextlib.closing(zipfile.ZipFile(path, "r")) as f:
xmlFile = f.open("lesson.xml", "r")
except (zipfile.BadZipfile, KeyError):
xmlFile = open(path, "r")
#crashes when the file structure is invalid.
try:
root = ElementTree.parse(xmlFile).getroot()
finally:
xmlFile.close()
items = []
for i, card in enumerate(root.findall(".//Card")):
item = {
"id": i,
"questions": self._parse(card.get("Frontside") or u""),
"answers": self._parse(card.get("Backside") or u""),
}
created = card.get("DateCreated")
if created:
item["created"] = self._parseDate(created)
items.append(item)
return {
"resources": {},
"list": {
"items": sorted(items, key=lambda item: item.get("created")),
"tests": [],
},
}
示例7: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
with open(path) as f:
root = ElementTree.parse(f).getroot()
#dutch: lijst = list
listTree = root.find("lijst")
wordList = {
"items": list(),
}
#dutch: titel = title
wordList["title"] = listTree.findtext("titel") or u""
#dutch: taal = language
wordList["questionLanguage"] = listTree.findtext("taal/a") or u""
wordList["answerLanguage"] = listTree.findtext("taal/b") or u""
#change locale temporary; so strptime can do it's work the way
#it should.
locale.setlocale(locale.LC_ALL, "C")
try:
created = datetime.datetime.strptime(
listTree.findtext("created").rsplit(" ", 1)[0], #strip tz info
"%a, %d %b %Y %H:%M:%S" #since our datetime objects are naive
)
except (ValueError, AttributeError):
created = None
#set locale back to make sure conflicts don't arise with other
#modules depending on the locale.
locale.resetlocale()
#counter is used as word id
counter = 1
#dutch: woord = word
for wordTree in listTree.findall("woord"):
word = {
"id": int(),
"questions": list(),
"answers": list(),
"comment": unicode()
}
word["id"] = counter
if created:
word["created"] = created
wsp = self._modules.default("active", type="wordsStringParser")
word["questions"] = wsp.parse(wordTree.findtext("a") or u"")
word["answers"] = wsp.parse(wordTree.findtext("b") or u"")
wordList["items"].append(word)
counter += 1
return {
"resources": {},
"list": wordList,
}
示例8: getFileTypeOf
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def getFileTypeOf(self, path):
if path.endswith(".t2k"):
root = ElementTree.parse(open(path)).getroot()
if root.find("message_data/items//item/answers[@type='4']") is not None:
return "topo"
else:
#also support other formats in the future? Well,
#everything that's left can be opened like it's of type 'words'...
return "words"
示例9: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
"""Loads a .t2k file into the OpenTeacher data structure.
http://www.teach2000.nl/help/00513_advanced_file_format.htm
"""
root = ElementTree.parse(open(path)).getroot()
if self.getFileTypeOf(path) == "topo":
return self._loadTopo(root)
else:
return self._loadWords(root)
示例10: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
if path.endswith(".pau"):
with open(path) as f:
root = ElementTree.parse(f).getroot()
else:
with contextlib.closing(gzip.open(path)) as f:
root = ElementTree.parse(f).getroot()
wordList = {
#only the first line, because a description can be pretty
#long in Pauker...
"title": (root.findtext("Description") or u"").split("\n")[0].strip(),
"items": [],
}
cards = root.findall("Batch//Card")
if cards is not None:
for id, card in enumerate(cards):
questions = self._parse(
(card.findtext("FrontSide") or u"").strip() or
(card.findtext("FrontSide/Text") or u"").strip() or
u""
)
answers = self._parse(
(card.findtext("BackSide") or u"").strip() or
(card.findtext("BackSide/Text") or u"").strip() or
(card.findtext("ReverseSide") or u"").strip() or
(card.findtext("ReverseSide/Text") or u"").strip() or
u""
)
wordList["items"].append({
"id": id,
"questions": questions,
"answers": answers
})
return {
"resources": {},
"list": wordList,
}
示例11: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, kgmPath):
#Feed the xml parser
with open(kgmPath) as f:
root = ElementTree.parse(f).getroot()
#load the map, in various later needed formats
mapPath = os.path.join(os.path.dirname(kgmPath), root.findtext("mapFile"))
mapImage = Image.open(mapPath).convert("RGB")
mapImageData = mapImage.load()
items = []
for counter, division in enumerate(root.findall("division")):
#iterate over all places ('divisions')
if division.findtext("ignore") == "yes":
#unimportant division: skip
continue
item = {
"id": counter,
"name": division.findtext("name") or u"",
}
#get the color the place has on the map
r = int(division.findtext("color/red"))
g = int(division.findtext("color/green"))
b = int(division.findtext("color/blue"))
color = (r, g, b)
#get the average pixel with that color. This is done by
#iterating over all pixels and using them to calculate an
#average if the color matches.
sumX = 0
sumY = 0
count = 0
for x in range(mapImage.size[0]):
for y in range(mapImage.size[1]):
if mapImageData[x, y] == color:
sumX += x
sumY += y
count += 1
#save the averages as coordinate.
item["x"] = sumX / count
item["y"] = sumY / count
items.append(item)
return {
"resources": {
"mapPath": mapPath,
},
"list": {
"items": items,
},
}
示例12: image2svg
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def image2svg(im_path, embed_image=True):
### Only part of the code that uses PIL ######
im = Image.open(im_path)
width, height = im.size
fmt = im.format.lower()
### End of PIL ###############################
if embed_image:
f = open(im_path, 'rb')
im_contents = f.read()
f.close()
b64 = base64.b64encode(im_contents)
#im_href = "data:image/" + fmt + ";base64," + base64.b64encode(im_contents)
im_href = "data:image/{0};base64,{1}".format(fmt, b64)
else:
im_href = "file:" + urllib.pathname2url(im_path)
### SVG ###
doc = etree.parse(blank_svg_path)
svg = doc.getroot()
svg.set('width', str(width))
svg.set('height', str(height))
svg.set('xmlns:xlink', "http://www.w3.org/1999/xlink")
### Use descriptive variables for the layers
image_layer = svg[image_layer_index]
shapes_layer = svg[shapes_layer_index]
### Create the 'image' element
image = etree.SubElement(image_layer, 'image')
image.set('x', '0')
image.set('y', '0')
image.set('height', str(height))
image.set('width', str(width))
image.set('xlink:href', im_href) # encode base64
###
svg_content = etree.tostring(svg) # remove
#### Very Ugly Hack Ahead !!!
hack_head, hack_body = svg_content.split('\n', 1)
hack_head = hack_head[:-1]
hack_head = ''.join([hack_head, ' xmlns="http://www.w3.org/2000/svg">'])
svg_content = '\n'.join([hack_head, hack_body])
#### END HACK
svg_b64 = "data:image/svg+xml;base64," + base64.b64encode(svg_content)
return {'svg': svg_content,
'svg_b64': svg_b64,
'height': height,
'width': width}
示例13: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
items = []
with open(path, "r") as f:
root = ElementTree.parse(f).getroot()
for id, card in enumerate(root.findall("card")):
items.append({
"id": id,
"questions": self._parse(card.findtext("front") or u""),
"answers": self._parse(card.findtext("back") or u""),
})
return {
"list": {
"items": items,
},
"resources": {},
}
示例14: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
"""Documentation: http://jvlt.sourceforge.net/download.html
(at the bottom)
"""
with contextlib.closing(zipfile.ZipFile(path)) as jvltZip:
root = ElementTree.parse(jvltZip.open("dict.xml")).getroot()
list = {"items": []}
for i, itemTree in enumerate(root.findall(".//entry")):
list["items"].append({
"id": i,
"questions": self._parse(itemTree.findtext("orth") or u""),
"answers": self._parse(itemTree.findtext("sense/trans") or u""),
"comment": u", ".join([pron.text for pron in itemTree.findall("pron")]),
})
return {
"resources": {},
"list": list,
}
示例15: load
# 需要导入模块: from etree import ElementTree [as 别名]
# 或者: from etree.ElementTree import parse [as 别名]
def load(self, path):
"""Tries to load .wcu CueCard files. Based on observation of the
file format, not on documentation. Seems like there is source
code available though.
"""
with open(path, "r") as f:
root = ElementTree.parse(f).getroot()
items = []
for id, card in enumerate(root.findall("Card")):
items.append({
"id": id,
"questions": self._parse(card.get("Question")),
"answers": self._parse(card.get("Answer")),
})
return {
"list": {
"items": items,
},
"resources": {},
}