elementtree

UnicodeEncodeError: 'ascii' codec can't encode characters

泄露秘密 提交于 2019-12-04 02:32:13
I have a dict that's feed with url response. Like: >>> d { 0: {'data': u'<p>found "\u62c9\u67cf \u591a\u516c \u56ed"</p>'} 1: {'data': u'<p>some other data</p>'} ... } While using xml.etree.ElementTree function on this data values ( d[0]['data'] ) I get the most famous error message: UnicodeEncodeError: 'ascii' codec can't encode characters... What should I do to this Unicode string to make it suitable for ElementTree parser? PS. Please don't send me links with Unicode & Python explanation. I read it all already unfortunately, and can't make use of it, as hopefully others can. You'll have to

Python 2.6.1 : expected path separator ([)

点点圈 提交于 2019-12-04 00:45:55
I am getting a path separator error in python 2.6.1. I have not found this issue with python 2.7.2 version, but unfortunately I need this in 2.6.1 only. Is there any another way to achieve the same? :( my code :- import xml.etree.ElementTree as ET #version 1.2.6 import sys class usersDetail(object): def __init__(self, users=None): self.doc = ET.parse("test.xml") self.root = self.doc.getroot() def final_xml(self,username): r = self.root.find("user[@username='user1']") #not working in 2.6.1 :( self.root.remove(r) print r tree = ET.ElementTree(self.root) tree.write("msl.xml") if __name__ == '_

ElementTree's iter() equivalent in Python2.6

本小妞迷上赌 提交于 2019-12-03 23:43:41
问题 I have this code with ElementTree that works well with Python 2.7. I needed to get all the nodes with the name "A" under "X/Y" node. from xml.etree.ElementTree import ElementTree verboseNode = topNode.find("X/Y") nodes = list(verboseNode.iter("A")) However, when I tried to run it with Python 2.6, I got this error. ionCalculateSkewConstraint.py", line 303, in getNodesWithAttribute nodes = list(startNode.iter(nodeName)) AttributeError: _ElementInterface instance has no attribute 'iter' It looks

Python: Ignore xmlns in elementtree.ElementTree

怎甘沉沦 提交于 2019-12-03 23:21:34
Is there a way to ignore the XML namespace in tage names in elementtree.ElementTree ? I try to print all technicalContact tags: for item in root.getiterator(tag='{http://www.example.com}technicalContact'): print item.tag, item.text And I get something like: {http://www.example.com}technicalContact blah@example.com But what I really want is: technicalContact blah@example.com Is there a way to display only the suffix (sans xmlns), or better - iterate over the elements without explicitly stating xmlns? You can define a generator to recursively search through your element tree in order to find

Error 'failed to load external entity' when using Python lxml

心已入冬 提交于 2019-12-03 22:20:33
I'm trying to parse an XML document I retrieve from the web, but it crashes after parsing with this error: ': failed to load external entity "<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="GreenButtonDataStyleSheet.xslt"?> That is the second line in the XML that is downloaded. Is there a way to prevent the parser from trying to load the external entity, or another way to solve this? This is the code I have so far: import urllib2 import lxml.etree as etree file = urllib2.urlopen("http://www.greenbuttondata.org/data/15MinLP_15Days.xml") data = file.read() file

How to extract xml attribute using Python ElementTree

 ̄綄美尐妖づ 提交于 2019-12-03 18:55:25
问题 For: <foo> <bar key="value">text</bar> </foo> How do I get "value"? xml.findtext("./bar[@key]") Throws an error. 回答1: This will find the first instance of an element named bar and return the value of the attribute key . In [52]: import xml.etree.ElementTree as ET In [53]: xml=ET.fromstring(contents) In [54]: xml.find('./bar').attrib['key'] Out[54]: 'value' 回答2: Your expression: ./bar[@key] It means: bar children having key attribute If you want to select the attribute, use this relative

How to correctly parse utf-8 xml with ElementTree?

情到浓时终转凉″ 提交于 2019-12-03 17:11:48
问题 I need help to understand why parsing my xml file* with xml.etree.ElementTree produces the following errors. * My test xml file contains arabic characters. Task: Open and parse utf8_file.xml file. My first try: import xml.etree.ElementTree as etree with codecs.open('utf8_file.xml', 'r', encoding='utf-8') as utf8_file: xml_tree = etree.parse(utf8_file) Result 1: UnicodeEncodeError: 'ascii' codec can't encode characters in position 236-238: ordinal not in range(128) My second try: import xml

lxml.etree, element.text doesn't return the entire text from an element

℡╲_俬逩灬. 提交于 2019-12-03 16:18:42
问题 I scrapped some html via xpath, that I then converted into an etree. Something similar to this: <td> text1 <a> link </a> text2 </td> but when I call element.text, I only get text1 (It must be there, when I check my query in FireBug, the text of the elements is highlighted, both the text before and after the embedded anchor elements... 回答1: Use element.xpath("string()") or lxml.etree.tostring(element, method="text") - see the documentation. 回答2: As a public service to people out there who may

Stripping python namespace attributes from an lxml.objectify.ObjectifiedElement [duplicate]

人走茶凉 提交于 2019-12-03 14:29:13
Possible Duplicate: When using lxml, can the XML be rendered without namespace attributes? How can I strip the python attributes from an lxml.objectify.ObjectifiedElement ? Example: In [1]: from lxml import etree, objectify In [2]: foo = objectify.Element("foo") In [3]: foo.bar = "hi" In [4]: foo.baz = 1 In [5]: foo.fritz = None In [6]: print etree.tostring(foo, pretty_print=True) <foo xmlns:py="http://codespeak.net/lxml/objectify/pytype" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" py:pytype="TREE"> <bar py:pytype="str">hi</bar> <baz py

Getting all instances of child node using xml.etree.ElementTree

不问归期 提交于 2019-12-03 13:49:02
问题 I have the following XML file as input: <Test> <callEvents> <moc> <causeForTermination>0</causeForTermination> <serviceCode> <teleServiceCode>11</teleServiceCode> </serviceCode> <dialledDigits>5555555</dialledDigits> <connectedNumber>77777</connectedNumber> </moc> <moc> <causeForTermination>0</causeForTermination> <serviceCode> <teleServiceCode>11</teleServiceCode> </serviceCode> <dialledDigits>2222222</dialledDigits> </moc> </callEvents> <callEventsCount>100</callEventsCount> </Test> I want