elementtree

ElementTree and unicode

坚强是说给别人听的谎言 提交于 2019-11-28 09:01:20
I have this char in an xml file: <data> <products> <color>fumè</color> </product> </data> I try to generate an instance of ElementTree with the following code: string_data = open('file.xml') x = ElementTree.fromstring(unicode(string_data.encode('utf-8'))) and I get the following error: UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 185: ordinal not in range(128) (NOTE: The position is not exact, I sampled the xml from a larger one). How to solve it? Thanks Martijn Pieters You do not need to decode XML for ElementTree to work. XML carries it's own encoding

ElementTree findall() returning empty list

自作多情 提交于 2019-11-28 08:59:53
I am trying to write a small script for interacting with the last.fm API. I have a small bit of experience working with ElementTree , but the way I used it previously doesn't seem to be working, it instead returns an empty list. I removed the API key as I don't know exactly how private it should be, and gave an example of the XML I am receiving in it's place. Class for interacting with API: from xml.etree import ElementTree import urllib import urllib2 class Last_fmWrapper(object): def __init__(self): self.last_fm_api_key = '*****************************' self.api_url = 'http://ws

Accessing XMLNS attribute with Python Elementree?

做~自己de王妃 提交于 2019-11-28 06:54:21
How can one access NS attributes through using ElementTree? With the following: <data xmlns="http://www.foo.net/a" xmlns:a="http://www.foo.net/a" book="1" category="ABS" date="2009-12-22"> When I try to root.get('xmlns') I get back None, Category and Date are fine, Any help appreciated.. Jeffrey Harris I think element.tag is what you're looking for. Note that your example is missing a trailing slash, so it's unbalanced and won't parse. I've added one in my example. >>> from xml.etree import ElementTree as ET >>> data = '''<data xmlns="http://www.foo.net/a" ... xmlns:a="http://www.foo.net/a" ..

ElementTree : Element.remove() jumping iteration

不问归期 提交于 2019-11-28 05:33:10
问题 I have this xml inputfile: <?xml version="1.0"?> <zero> <First> <second> <third-num>1</third-num> <third-def>object001</third-def> <third-len>458</third-len> </second> <second> <third-num>2</third-num> <third-def>object002</third-def> <third-len>426</third-len> </second> <second> <third-num>3</third-num> <third-def>object003</third-def> <third-len>998</third-len> </second> </First> </zero> My goal is to remove any second level for which <third-def> that is not a value. To do that, I wrote

Use xml.etree.elementtree to print nicely formatted xml files [duplicate]

喜你入骨 提交于 2019-11-28 04:50:56
This question already has an answer here: Pretty printing XML in Python 20 answers I am trying to use xml.etree.elementtree to write out xml files with Python. The issue is that they keep getting generated in a single line. I want to be able to easily reference them so if its possible I would really like to be able to have the written out cleanly. This is what I am getting <Language><En><Port>Port</Port><UserName>UserName</UserName></En><Ch><Port>IP地址</Port><UserName>用户名称</UserName></Ch></Language> This is what I would like to see. <Language> <En> <Port>Port</Port> <UserName>UserName</UserName

Emitting namespace specifications with ElementTree in Python

拜拜、爱过 提交于 2019-11-28 04:32:04
I am trying to emit an XML file with element-tree that contains an XML declaration and namespaces. Here is my sample code: from xml.etree import ElementTree as ET ET.register_namespace('com',"http://www.company.com") #some name # build a tree structure root = ET.Element("STUFF") body = ET.SubElement(root, "MORE_STUFF") body.text = "STUFF EVERYWHERE!" # wrap it in an ElementTree instance, and save as XML tree = ET.ElementTree(root) tree.write("page.xml", xml_declaration=True, method="xml" ) However, neither the <?xml tag comes out nor any namespace/prefix information. I'm more than a little

ElementTree iterparse strategy

谁说我不能喝 提交于 2019-11-28 04:24:32
I have to handle xml documents that are big enough (up to 1GB) and parse them with python. I am using the iterparse() function (SAX style parsing). My concern is the following, imagine you have an xml like this <?xml version="1.0" encoding="UTF-8" ?> <families> <family> <name>Simpson</name> <members> <name>Homer</name> <name>Marge</name> <name>Bart</name> </members> </family> <family> <name>Griffin</name> <members> <name>Peter</name> <name>Brian</name> <name>Meg</name> </members> </family> </families> The problem is, of course to know when I am getting a family name (as Simpsons) and when I am

Convert Python ElementTree to string

不打扰是莪最后的温柔 提交于 2019-11-28 03:45:54
Whenever I call ElementTree.tostring(e) , I get the following error message: AttributeError: 'Element' object has no attribute 'getroot' Is there any other way to convert an ElementTree object into an XML string? TraceBack: Traceback (most recent call last): File "Development/Python/REObjectSort/REObjectResolver.py", line 145, in <module> cm = integrateDataWithCsv(cm, csvm) File "Development/Python/REObjectSort/REObjectResolver.py", line 137, in integrateDataWithCsv xmlstr = ElementTree.tostring(et.getroot(),encoding='utf8',method='xml') AttributeError: 'Element' object has no attribute

Edit XML file text based on path

感情迁移 提交于 2019-11-28 03:06:40
问题 I have an XML file (e.g. jerry.xml) which contains some data as given below. <data> <country name="Peru"> <rank updated="yes">2</rank> <language>english</language> <currency>1.21$/kg</currency> <gdppc month="06">141100</gdppc> <gdpnp month="10">2.304e+0150</gdpnp> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank updated="yes">5</rank> <language>english</language> <currency>4.1$/kg</currency> <gdppc month="05"

Find element by text with XPath in ElementTree

亡梦爱人 提交于 2019-11-28 01:49:43
Given an XML like the following: <root> <element>A</element> <element>B</element> </root> How can I match the element with content A using ElementTree and its support for XPath? Thanks AFAIK ElementTree does not support XPath. Has it changed? Anyway, you can use lxml and the following XPath expression: import lxml.etree doc = lxml.etree.parse('t.xml') print doc.xpath('//element[text()="A"]')[0].text print doc.xpath('//element[text()="A"]')[0].tag The result will be: A element If you want to use the standard library ElementTree , rather than lxml, you can use iteration to find all sub elements