本文整理汇总了Python中xml.etree.cElementTree.XML.getiterator方法的典型用法代码示例。如果您正苦于以下问题:Python XML.getiterator方法的具体用法?Python XML.getiterator怎么用?Python XML.getiterator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree.cElementTree.XML
的用法示例。
在下文中一共展示了XML.getiterator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse_captions
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def parse_captions(soup):
""" Converts custom iView captions into SRT format, usable in most
decent media players.
"""
# Horrible hack to escape literal ampersands, which have been seen in
# some captions XML. Inspired by
# http://stackoverflow.com/questions/6088760/fix-invalid-xml-with-ampersands-in-python
if b"<![CDATA[" not in soup: # Not seen, but be future proof
soup = re.sub(b"&(?![#\w]+;)", b"&", soup)
xml = XML(soup)
output = ''
i = 1
for title in xml.getiterator('title'):
start = title.get('start')
ids = start.rfind(':')
end = title.get('end')
ide = end.rfind(':')
output = output + str(i) + '\n'
output = output + start[:ids] + ',' + start[ids+1:] + ' --> ' + end[:ide] + ',' + end[ide+1:] + '\n'
output = output + title.text.replace('|','\n') + '\n\n'
i += 1
return output
示例2: parse_config
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def parse_config(soup):
""" There are lots of goodies in the config we get back from the ABC.
In particular, it gives us the URLs of all the other XML data we
need.
"""
xml = XML(soup)
params = dict()
for param in xml.getiterator('param'):
params.setdefault(param.get('name'), param.get('value'))
# should look like "rtmp://cp53909.edgefcs.net/ondemand"
# Looks like the ABC don't always include this field.
# If not included, that's okay -- ABC usually gives us the server in the auth result as well.
rtmp_url = params['server_streaming']
rtmp_chunks = rtmp_url.split('/')
return {
'rtmp_url' : rtmp_url,
'rtmp_host' : rtmp_chunks[2],
'rtmp_app' : rtmp_chunks[3],
'auth_url' : params['auth'],
'api_url' : params['api'],
'categories_url' : params['categories'],
'captions_url' : params['captions'],
}
示例3: parse_captions
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def parse_captions(soup):
""" Converts custom iView captions into SRT format, usable in most
decent media players.
"""
# Horrible hack to escape literal ampersands, which have been seen in
# some captions XML. Inspired by
# http://stackoverflow.com/questions/6088760/fix-invalid-xml-with-ampersands-in-python
if b"<![CDATA[" not in soup: # Not seen, but be future proof
soup = re.sub(b"&(?![#\w]+;)", b"&", soup)
xml = XML(soup)
output = ''
i = 1
for title in xml.getiterator('title'):
start = title.get('start')
(start, startfract) = start.rsplit(':', 1)
end = title.get('end')
(end, endfract) = end.rsplit(':', 1)
output = output + '{}\n'.format(i)
output = output + '{},{:0<3.3} --> {},{:0<3.3}\n'.format(start, startfract, end, endfract)
output = output + title.text.replace('|','\n') + '\n\n'
i += 1
return output
示例4: get_raw_text
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def get_raw_text(pthFile):
"""
gets a path to a file as an argument and returns a list containing
the paragraphs of the word document file
"""
"""
Constants used to iterate over the XML tree
"""
WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
PARA = WORD_NAMESPACE + 'p'
TEXT = WORD_NAMESPACE + 't'
docWordDoc = zipfile.ZipFile(pthFile) #gets the documents of the word
xmlContent = docWordDoc.read('word/document.xml') #access the xml file
docWordDoc.close()
treeXML = XML(xmlContent) #parses the xml content into a tree that will be further used to access the text
lstParagraphs = [] #output list with the paragraphs of the text
#now we proceed to extract the text from the tree
#the idea is to iterate over the tree and
#for each node that contains text, substract it and add it to
#the output
for parParagraph in treeXML.getiterator(PARA):
lstTexts = [nodElement.text
for nodElement in parParagraph.getiterator(TEXT)
if nodElement.text]
if lstTexts:
print lstTexts
lstParagraphs.append(''.join(lstTexts))
return lstParagraphs
示例5: get_docx_text
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def get_docx_text(self, path):
document = zipfile.ZipFile(path)
xml_content = document.read('word/document.xml')
tree = XML(xml_content)
document.close()
paragraphs = []
for paragraph in tree.getiterator(para_tag):
texts = [node.text for node in paragraph.getiterator(text_tag) if node.text]
if texts:
paragraphs.append(''.join(texts))
return paragraphs
示例6: get_docx_text
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def get_docx_text(path):
document = zipfile.ZipFile(path)
xml_content = document.read("word/document.xml")
document.close()
tree = XML(xml_content)
paragraphs = []
for paragraph in tree.getiterator(PARA):
texts = [node.text for node in paragraph.getiterator(TEXT) if node.text]
if texts:
paragraphs.append("".join(texts))
return paragraphs
示例7: get_docx_text
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def get_docx_text(path):
"""
Take the path of a docx file as argument, return the text in unicode
in the form of a list.
"""
document = zipfile.ZipFile(path)
xml_content = document.read('word/document.xml')
document.close()
tree = XML(xml_content)
paragraphs = []
for paragraph in tree.getiterator(PARA):
texts = [node.text.encode('utf-8')
for node in paragraph.getiterator(TEXT)
if node.text]
if texts:
paragraphs.append(''.join(texts))
return paragraphs
# def get_docx_text(path):
# """
# Take the path of a docx file as argument, return the text in unicode
# in the form of a list.
# """
# document = zipfile.ZipFile(path)
# xml_content = document.read('word/document.xml')
# document.close()
# tree = XML(xml_content)
# sections = []
# for section in tree.getiterator(SECT):
# paragraphs = []
# for paragraph in section.getiterator(PARA):
# print 'para'
# texts = [node.text.encode('utf-8')
# for node in paragraph.getiterator(TEXT)
# if node.text]
# if texts:
# paragraphs.append(''.join(texts))
# print str(paragraphs)
# if paragraphs:
# sections.append(''.join(paragraphs))
# return sections
示例8: read_docx
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def read_docx(file,document,path,trie):
xml_content = document.read('word/document.xml')
document.close()
tree = XML(xml_content)
paragraphs = ""
for paragraph in tree.getiterator(PARA):
texts=""
for node in paragraph.getiterator(TEXT):
if node.text:
texts += node.text.replace('\u7460',"")
if texts:
paragraphs+=str(texts)
#print(paragraphs)
string_spilt(paragraphs,path,trie)
trie.insert_doc_len(path,len(file)+len(paragraphs))
示例9: get_doc_text
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def get_doc_text(path):
"""
Take the path of a docx or a dot file as argument, return the text in unicode.
"""
if "docx" == path[-4:]:
document = zipfile.ZipFile(path)
xml_content = document.read('word/document.xml')
document.close()
tree = XML(xml_content)
#print tree
paragraphs = []
for paragraph in tree.getiterator(PARA):
texts = [node.text for node in paragraph.iter(TEXT) if node.text]
if texts:
paragraphs.append(''.join(texts))
pass
pass
#print paragraphs
return paragraphs
#
elif "odt" == path[-3:]:
document = zipfile.ZipFile(path)
xml_content = document.read('content.xml')
document.close()
doc = xml.dom.minidom.parseString(xml_content)
print(" doc: ",doc)
print("doc::end")
#paras = doc.getElementsByTagName('text:span')
#paras = doc.getElementsByTagName('text:p')
#
# we get here all elements Headers, text and table components:
#
paras = doc.getElementsByTagName("*")
print("I have ", len(paras), " paragraphs ")
paragraphs = []
for p in paras:
for ch in p.childNodes:
if ch.nodeType == ch.TEXT_NODE:
paragraphs.append(''.join(ch.wholeText))
pass
pass
pass
print(paragraphs)
return paragraphs
else:
print()
raise Warning("only docx and odt files are handled")
示例10: get_docx_text
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def get_docx_text(path):
""" Take the path of a docx file as argument, return the text in unicode."""
document = zipfile.ZipFile(path)
xml_content = document.read('word/document.xml')
document.close()
tree = XML(xml_content)
paragraphs = []
for paragraph in tree.getiterator(PARA):
texts = [node.text
for node in paragraph.getiterator(TEXT)
if node.text]
if texts:
paragraphs.append(''.join(texts))
return '\n\n'.join(paragraphs)
示例11: docx_do_docx
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def docx_do_docx(azip, afile):
word_namespace = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
par = word_namespace + "p"
txt = word_namespace + "t"
xml_content = azip.read("word/document.xml")
tree = XML(xml_content)
paragraphs = []
for paragraph in tree.getiterator(par):
texts = [node.text for node in paragraph.getiterator(txt) if node.text]
if texts:
paragraphs.append("".join(texts))
text = "\n\n".join(paragraphs)
text_do_data(text, afile)
示例12: docx_do_docx
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def docx_do_docx(azip, afile):
namespace = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
par = namespace + 'p'
txt = namespace + 't'
xml_content = azip.read('word/document.xml')
tree = XML(xml_content)
paragraphs = []
for paragraph in tree.getiterator(par):
texts = [node.text for node in paragraph.getiterator(txt)
if node.text]
if texts:
paragraphs.append(''.join(texts))
text = '\n\n'.join(paragraphs)
text_do_data(text, afile)
示例13: getContents
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def getContents(self):
"""
Just read the paragraphs from an XML file.
"""
xml_content = self.my_docx.read('word/document.xml')
self.my_docx.close()
tree = XML(xml_content)
self.text_in_paragraphs = []
for paragraph in tree.getiterator(PARA):
texts = [node.text for node in paragraph.iter(TEXT) if node.text]
if texts:
self.text_in_paragraphs.append(''.join(texts))
pass
pass
#print paragraphs
return self.text_in_paragraphs
示例14: parse_captions
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def parse_captions(soup):
""" Converts custom iView captions into SRT format, usable in most
decent media players.
"""
xml = XML(soup)
output = ''
i = 1
for title in xml.getiterator('title'):
start = title.get('start')
ids = start.rfind(':')
end = title.get('end')
ide = end.rfind(':')
output = output + str(i) + '\n'
output = output + start[:ids] + ',' + start[ids+1:] + ' --> ' + end[:ide] + ',' + end[ide+1:] + '\n'
output = output + title.text.replace('|','\n') + '\n\n'
i += 1
return output
示例15: get_docx_text
# 需要导入模块: from xml.etree.cElementTree import XML [as 别名]
# 或者: from xml.etree.cElementTree.XML import getiterator [as 别名]
def get_docx_text(path):
"""
Take the path of a docx file as argument, return the text in unicode.
"""
WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}' # formatting for docx
PARA = WORD_NAMESPACE + 'p' # formatting for paragraphs
TEXT = WORD_NAMESPACE + 't' # formatting for text
document = zipfile.ZipFile(path) # the unzipped document path
xml_content = document.read('word/document.xml') # location of the primary xml document
document.close() # closes the document
tree = XML(xml_content) # splits the xl into a tree
paragraphs = [] # a list of the paragraphs
for paragraph in tree.getiterator(PARA): # for every new paragraph in the tree
texts = [node.text # the text is the text node in the tree
for node in paragraph.getiterator(TEXT) #
if node.text] # if the node is text, add it to the text list
if texts: # if a text is found,
paragraphs.append(''.join(texts)) # add it to the paragraphs list
#return('\n\n'.join(paragraphs))
return(paragraphs) # return the paragra