elementtree

How to get the child of child using Python's ElementTree

为君一笑 提交于 2019-12-12 01:53:59
问题 I'm building a Python file that communicates with a PLC. When compiling, the PLC creates a XML file that delivers important information about the program. The XML looks more less like this: <visu> <time>12:34</time> <name>my_visu</name> <language>english</language> <vars> <var name="input1">2</var> <var name="input2">45.6</var> <var name="input3">"hello"</var> </vars> </visu> The important part is found under child "vars". Using Python I want to make a file that when sending argument "input2"

migrate from ET.parse to etree.iterparse

亡梦爱人 提交于 2019-12-12 01:52:41
问题 Wrote a code to parse .osm file. Spend a lot of time to build a up to 50 rows code but ran into a 'Memory Error' problem. Seems like the best solution is to use interparse() instead of parse(). My question is: how should I change my code (strating of my code) import xml.etree.ElementTree as ET tree = ET.parse('file.osm') root = tree.getroot() to ( using interparse() method) (not my code) import xml.etree.ElementTree as etree context=etree.iterparse('file.osm', events=('start', 'end', 'start

Elementtree displaying elements out of order

断了今生、忘了曾经 提交于 2019-12-12 01:49:37
问题 I'm using Python's ElementTree to parse xml files. I have a "findall" to find all "revision" subelements, but when I iterate through the result, they are not in document order. What can I be doing wrong? Here's my code: allrevisions = page.findall('{http://www.mediawiki.org/xml/export-0.5/}revision') for rev in allrevisions: print rev print rev.find('{http://www.mediawiki.org/xml/export-0.5/}timestamp').text Here's a link to the document I'm parsing: http://pastie.org/2780983 Thanks, bsg

How to extract text in nested xml after closing of one tag in python using xml.etree.ElementTree

时光毁灭记忆、已成空白 提交于 2019-12-11 23:07:03
问题 I want to extract all text in xml document, and I am having a problem for the following case: ... <a> hello <B> there </B> How was your day. ..... </a> In this snippet, I can get the text "hello" and "there" because I can get them using the following tags: a.text b.text but I don't know how to access the "How was your day." part. 回答1: You are looking for the .tail attribute of an element: >>> from xml.etree import ElementTree >>> example = ElementTree.fromstring('''\ ... <a> ... hello ... <B>

Can't parse XML effectively using Python

喜欢而已 提交于 2019-12-11 18:46:08
问题 import urllib import xml.etree.ElementTree as ET def getWeather(city): #create google weather api url url = "http://www.google.com/ig/api?weather=" + urllib.quote(city) try: # open google weather api url f = urllib.urlopen(url) except: # if there was an error opening the url, return return "Error opening url" # read contents to a string s = f.read() tree=ET.parse(s) current= tree.find("current_condition/condition") condition_data = current.get("data") weather = condition_data if weather == "<

findall() takes exactly 2 arguments (3 given)

柔情痞子 提交于 2019-12-11 18:15:41
问题 here's my code: from elementTree.elementtree.ElementTree import Element, parse, ElementTree f = open("myxml.xml", 'r') tree = parse(f) root = tree.getroot() f2 = open('out.xml', 'w') print tree print root props = root.findall('property') print props tree.write(f2) Stacktrace: Traceback (most recent call last): File "xmlpy.py", line 11, in <module> props = root.findall('property') File "D:\user-testing-areas\DWJ\py\elementTree\elementtree\ElementTree.py", li ne 390, in findall return

Parsing XHTML using xml.etree.ElementTree

≯℡__Kan透↙ 提交于 2019-12-11 13:15:28
问题 I want to use xml.etree.ElementTree to parse an XHTML document in Python 3. The document contains   entities, so I cannot use the default parser settings. I'd like to do something similar to: with urllib.request.urlopen(BASE_URL) as url: body = url.read() parser = ET.XMLParser() parser.parser.UseForeignDTD(True) parser.entity.update(entitydefs) etree = ET.ElementTree() root = etree.fromstring(body) But fromstring is a free function in ElementTree . How can I achieve something similar with

Avoid writing < character to XML in python

左心房为你撑大大i 提交于 2019-12-11 12:47:53
问题 I am trying to write this text value to the XML tag like this <Parameter name="name"><![CDATA[xyzvalue]]></Parameter> Whenever, i am setting this value as text to this tag. It generates it like this <Parameter name="name"><![CDATA[xyzvalue]]></Parameter> I need have tried to avoid unescape characters in the code ET.SubElement(parameters, "Parameter", name="id").text = unescape("<![CDATA[xyzvalue]]>") How can i avoid it writing like this to the xml file ? I need to avoid escaping characters in

Parse a *.nfo file with python

喜欢而已 提交于 2019-12-11 12:03:44
问题 I try to parse a nfo file and print in a html code style (a table). I tried with xml.etree but i get only 2 elements: Metadata and Category . This is how a .nfo looks like: <?xml version="1.0"?> <MsInfo> <Metadata> <Version>8.0</Version> <CreationUTC>12/02/15 10:45:25</CreationUTC> </Metadata> <Category name="System Summary"> <Data> <Item><![CDATA[OS Name]]></Item> <Value><![CDATA[Microsoft Windows 8.1 Pro]]></Value> </Data> </Category> </MsInfo> My code looks like: tree = ET.parse(File) root

python lxml write to file in predefined order

自作多情 提交于 2019-12-11 11:56:15
问题 I want to write following lxml etree subelements : <ElementProtocolat0x3803048>, <ElementStudyEventDefat0x3803108>, <ElementFormDefat0x3803248>, <ElementItemGroupDefat0x38032c8>, <ElementClinicalDataat0x3803408>, <ElementItemGroupDataat0x38035c8>, <ElementFormDefat0x38036c8>, to my odm xml file in a predefined order . i.e. <ElementProtocolat0x3803048>, <ElementStudyEventDefat0x3803108>, <ElementFormDefat0x3803248>, <ElementFormDefat0x38036c8>, <ElementItemGroupDefat0x38032c8>,