本文整理汇总了Python中lxml.etree.tounicode方法的典型用法代码示例。如果您正苦于以下问题:Python etree.tounicode方法的具体用法?Python etree.tounicode怎么用?Python etree.tounicode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.etree
的用法示例。
在下文中一共展示了etree.tounicode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_do_undo_element_double_format
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def test_do_undo_element_double_format(self):
replacer = formatting.PlaceholderMaker(['p'], ['b', 'u'])
# Formatting tags get replaced, and the content remains
text = u'<p>This is <u>doubly <b>formatted</b></u> text.</p>'
element = etree.fromstring(text)
replacer.do_element(element)
self.assertEqual(
element.text,
u'This is \ue006doubly \ue008formatted\ue007'
u'\ue005 text.')
replacer.undo_element(element)
result = etree.tounicode(element)
self.assertEqual(result, text)
示例2: _le_xml
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def _le_xml(self, arquivo):
if arquivo is None:
return False
if not isinstance(arquivo, basestring):
arquivo = etree.tounicode(arquivo)
if arquivo is not None:
if isinstance(arquivo, basestring):
if NAMESPACE_NFSE in arquivo:
arquivo = por_acentos(arquivo)
if u'<' in arquivo:
self._xml = etree.fromstring(tira_abertura(arquivo))
else:
arq = open(arquivo)
txt = ''.join(arq.readlines())
txt = tira_abertura(txt)
arq.close()
self._xml = etree.fromstring(txt)
else:
self._xml = etree.parse(arquivo)
return True
return False
示例3: execute
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def execute(url):
html = etree.parse(url)
#print(etree.tounicode(html))
root = html.getroot()
#print(root)
for tag in root:
#print('tag:', tag.tag)
#for subtag in tag:
# print('subtag:', subtag.tag, '=', subtag.text)
if tag.tag == 'pozycja':
print( [subtag.text for subtag in tag if tag.tag == 'pozycja'] )
#print('-----')
示例4: store
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def store(self, extension):
if self._last_page_id:
self._store_page(extension, self._last_page_id)
self._last_page_id = None
else:
for xml, page in tqdm(zip(self.xmlfiles, self.pages), desc="Writing PageXML files", total=len(self.xmlfiles)):
with open(split_all_ext(xml)[0] + extension, 'w') as f:
f.write(etree.tounicode(page.getroottree()))
示例5: _store_page
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def _store_page(self, extension, page_id):
page = self.pages[self.xmlfiles.index(page_id)]
with open(split_all_ext(page_id)[0] + extension, 'w') as f:
f.write(etree.tounicode(page.getroottree()))
示例6: clean_steps
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def clean_steps(self, nodes):
# HTML tag <p/>
re_p = re.compile('</?p[^>]*>')
# HTML tag <br/>
re_br = re.compile('<br\s*?/?>')
steps = [{
'step': idx + 1,
'desc': re_br.sub('\n', re_p.sub('', etree.tounicode(node.find('p')).strip())).strip(),
'img': node.find('img').get('src') if node.find('img') is not None else ''
} for idx, node in enumerate(nodes)]
return steps
示例7: patch_text
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def patch_text(actions, tree):
"""Takes a string with XML and a string with actions"""
tree = etree.fromstring(tree)
actions = patch.DiffParser().parse(actions)
tree = patch_tree(actions, tree)
return etree.tounicode(tree)
示例8: patch_file
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def patch_file(actions, tree):
"""Takes two filenames or streams, one with XML the other a diff"""
tree = etree.parse(tree)
if isinstance(actions, six.string_types):
# It's a string, so it's a filename
with open(actions) as f:
actions = f.read()
else:
# We assume it's a stream
actions = actions.read()
actions = patch.DiffParser().parse(actions)
tree = patch_tree(actions, tree)
return etree.tounicode(tree)
示例9: get_placeholder
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def get_placeholder(self, element, ttype, close_ph):
tag = etree.tounicode(element)
ph = self.tag2placeholder.get((tag, ttype, close_ph))
if ph is not None:
return ph
self.placeholder += 1
ph = six.unichr(self.placeholder)
self.placeholder2tag[ph] = PlaceholderEntry(element, ttype, close_ph)
self.tag2placeholder[tag, ttype, close_ph] = ph
return ph
示例10: render
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def render(self, result):
return etree.tounicode(result, pretty_print=self.pretty_print)
示例11: test_do_element
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def test_do_element(self):
replacer = formatting.PlaceholderMaker(['p'], ['b'])
# Formatting tags get replaced, and the content remains
text = u'<p>This is a tag with <b>formatted</b> text.</p>'
element = etree.fromstring(text)
replacer.do_element(element)
self.assertEqual(
etree.tounicode(element),
u'<p>This is a tag with \ue006formatted\ue005 text.</p>')
replacer.undo_element(element)
self.assertEqual(etree.tounicode(element), text)
# Non formatting tags get replaced with content
text = u'<p>This is a tag with <foo>formatted</foo> text.</p>'
element = etree.fromstring(text)
replacer.do_element(element)
result = etree.tounicode(element)
self.assertEqual(
result,
u'<p>This is a tag with \ue007 text.</p>')
# Single formatting tags still get two placeholders.
text = u'<p>This is a <b/> with <foo/> text.</p>'
element = etree.fromstring(text)
replacer.do_element(element)
result = etree.tounicode(element)
self.assertEqual(
result,
u'<p>This is a \ue009\ue008 with \ue00a text.</p>')
示例12: _test
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def _test(self, start, action, end):
tree = etree.fromstring(start)
self.patcher.handle_action(action, tree)
self.assertEqual(etree.tounicode(tree), end)
示例13: test_all_feed_with_one_item
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def test_all_feed_with_one_item(self):
response = self.app.get('/results/all.atom')
root = etree.XML(response.content)
xml_pretty = etree.tounicode(root, pretty_print=True)
result_event = ResultEvent.objects.first()
expected = '''<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
<title>Election results from example.com (with extra data)</title>
<link href="http://example.com/" rel="alternate"/>
<link href="http://example.com/results/all.atom" rel="self"/>
<id>http://example.com/</id>
<updated>{updated}</updated>
<entry>
<title>Tessa Jowell (Labour Party) won in Member of Parliament for Dulwich and West Norwood</title>
<link href="http://example.com/#{item_id}" rel="alternate"/>
<published>{updated}</published>
<updated>{updated}</updated>
<author>
<name>john</name>
</author>
<id>http://example.com/#{item_id}</id>
<summary type="html">A example.com volunteer recorded at {space_separated} that Tessa Jowell (Labour Party) won the ballot in Member of Parliament for Dulwich and West Norwood, quoting the source 'Seen on the BBC news').</summary>
<post_id>65808</post_id>
<winner_person_id>4322</winner_person_id>
<winner_person_name>Tessa Jowell</winner_person_name>
<winner_party_id>party:53</winner_party_id>
<winner_party_name>Labour Party</winner_party_name>
<user_id>{user_id}</user_id>
<post_name>Member of Parliament for Dulwich and West Norwood</post_name>
<information_source>Seen on the BBC news</information_source>
<parlparse_id>uk.org.publicwhip/person/123456</parlparse_id>
</entry>
</feed>
'''.format(
updated=rfc3339_date(result_event.created),
space_separated=result_event.created.strftime("%Y-%m-%d %H:%M:%S"),
item_id=result_event.id,
user_id=self.user.id,
)
self.compare_xml(expected, xml_pretty)
示例14: test_all_basic_feed_with_one_item
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def test_all_basic_feed_with_one_item(self):
response = self.app.get('/results/all-basic.atom')
root = etree.XML(response.content)
xml_pretty = etree.tounicode(root, pretty_print=True)
result_event = ResultEvent.objects.first()
expected = '''<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
<title>Election results from example.com</title>
<link href="http://example.com/" rel="alternate"/>
<link href="http://example.com/results/all-basic.atom" rel="self"/>
<id>http://example.com/</id>
<updated>{updated}</updated>
<entry>
<title>Tessa Jowell (Labour Party) won in Member of Parliament for Dulwich and West Norwood</title>
<link href="http://example.com/#{item_id}" rel="alternate"/>
<published>{updated}</published>
<updated>{updated}</updated>
<author>
<name>john</name>
</author>
<id>http://example.com/#{item_id}</id>
<summary type="html">A example.com volunteer recorded at {space_separated} that Tessa Jowell (Labour Party) won the ballot in Member of Parliament for Dulwich and West Norwood, quoting the source 'Seen on the BBC news').</summary>
</entry>
</feed>
'''.format(
updated=rfc3339_date(result_event.created),
space_separated=result_event.created.strftime("%Y-%m-%d %H:%M:%S"),
item_id=result_event.id,
)
self.compare_xml(expected, xml_pretty)
示例15: get_xml_reply
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import tounicode [as 别名]
def get_xml_reply(self):
return etree.tounicode(self.reply)