elementtree

lxml.etree iterparse() and parsing element completely

落爺英雄遲暮 提交于 2019-12-11 10:36:52
问题 I have an XML file with nodes that looks like this: <trkpt lat="-37.7944415" lon="144.9616159"> <ele>41.3681107</ele> <time>2015-04-11T03:52:33.000Z</time> <speed>3.9598</speed> </trkpt> I am using lxml.etree.iterparse() to iteratively parse the tree. I loop over each trkpt element's children and want to print the text value of the children nodes. E.g. for event, element in etree.iterparse(infile, events=("start", "end")): if element.tag == NAMESPACE + 'trkpt': for child in list(element):

Modify a XML using ElementTree

半腔热情 提交于 2019-12-11 10:08:35
问题 <grandParent> <parent> <child>Sam/Astronaut</child> </parent> </grandParent> I want to modify the above XML by adding another child tag inside parent tag. I'm doing something like this.. tree = ET.parse("test.xml") a=ET.Element('parent') b=ET.SubElement(a,"child") b.text="Jay/Doctor" tree.write("test.xml") Is this the correct way of modifying the xml file? Any better way? or what else should I be taking care of in the above code? 回答1: Your code creates a whole new tree and adds Jay to it. You

Python get XML siblings into dictionary

一个人想着一个人 提交于 2019-12-11 09:09:35
问题 I have an xml that looks like this: <root> <G> <G1>1</G1> <G2>some text</G2> <G3>some text</G3> <GP> <GP1>1</GP1> <GP2>a</GP2> <GP3>a</GP3> </GP> <GP> <GP1>2</GP1> <GP2>b</GP2> <GP3>b</GP3> </GP> <GP> <GP1>3</GP1> <GP2>c</GP2> <GP3>c</GP3> </GP> </G> <G> <G1>2</G1> <G2>some text</G2> <G3>some text</G3> <GP> <GP1>1</GP1> <GP2>aa</GP2> <GP3>aa</GP3> </GP> <GP> <GP1>2</GP1> <GP2>bb</GP2> <GP3>bb</GP3> </GP> <GP> <GP1>3</GP1> <GP2>cc</GP2> <GP3>cc</GP3> </GP> </G> <G> <G1>3</G1> <G2>some text</G2

Python ElementTree XML IOError: [Errno 22] invalid mode ('rb') or filename

独自空忆成欢 提交于 2019-12-11 08:47:27
问题 With the following code: import xml.etree.cElementTree as ET tree = ET.parse(r'https://apitest.batchbook.com/api/v1/people.xml?auth_token=GR5doLv88FrnLyLGIwok') I get the error message: IOError Traceback (most recent call last) <ipython-input-10-d91d452da3e7> in <module>() ----> 1 tree = ET.parse(r'https://apitest.batchbook.com/api/v1/people.xml?auth_token=GR5doLv88FrnLyLGIwok') <string> in parse(source, parser) <string> in parse(self, source, parser) IOError: [Errno 22] invalid mode ('rb')

Parsing XML using Python ElementTree

大兔子大兔子 提交于 2019-12-11 07:25:55
问题 I have an XML document in the following format <root> <H D="14/11/2017"> <FC> <F LV="0">The quick</F> <F LV="1">brown</F> <F LV="2">fox</F> </FC> </H> <H D="14/11/2017"> <FC> <F LV="0">The lazy</F> <F LV="1">fox</F> </FC> </H> </root> How can I extract the text from 'D' inside H tag and also all the text inside the F tags. 回答1: From ElementTree docs: We can import this data by reading from a file: import xml.etree.ElementTree as ET tree = ET.parse('country_data.xml') root = tree.getroot() Or

text extraction using python lxml looping issue

只谈情不闲聊 提交于 2019-12-11 06:27:32
问题 Here is a part of my xml file.. - <a:p> - <a:pPr lvl="2"> - <a:spcBef> <a:spcPts val="200" /> </a:spcBef> </a:pPr> - <a:r> <a:rPr lang="en-US" sz="1400" dirty="0" smtClean="0" /> <a:t>The</a:t> </a:r> - <a:r> <a:rPr lang="en-US" sz="1400" dirty="0" /> <a:t>world</a:t> </a:r> - <a:r> <a:rPr lang="en-US" sz="1400" dirty="0" smtClean="0" /> <a:t>is small</a:t> </a:r> </a:p> - <a:p> - <a:pPr lvl="2"> - <a:spcBef> <a:spcPts val="200" /> </a:spcBef> </a:pPr> - <a:r> <a:rPr lang="en-US" sz="1400"

xml to string and string to xml

蹲街弑〆低调 提交于 2019-12-11 05:49:48
问题 I am trying to convert xml to string and string to xml. Input xml file: <?xml version="1.0"?> <records> <record id="1"> <url>https://www.google.com</url> <data> <a xmlns="http://localhost:8080/"> <b>1</b> <c>Mayur</c> </a> </data> </record> <record id="2"> <url>https://www.google.com</url> <data> <a xmlns="http://localhost:8080/"> <b>2</b> <c>chavda</c> </a> </data> </record> </records> I can't get same xml file when i convert string to xml tree = ET.parse(xmlfilepath) root = tree.getroot()

How to order xml element attributes in Python?

半世苍凉 提交于 2019-12-11 04:26:58
问题 When parsing an xml file into a Python ElementTree the attributes' order is mixed up because Python stores the attributes in a dictionary. How can I change the order of the attributes in the dictionary? 回答1: Your self-answer is as you said long and cumbersome. It doesn't need to be. Also it will fail if (1) there are more than 10 keys (2) a dict has fewer keys than than expected. Try this; it's much simpler: >>> ordered_keys = ('z', 'y', 'e', 'x', 'w') # possible keys, in desired order Note:

Outputting an “unused” XML namespace using ElementTree

删除回忆录丶 提交于 2019-12-11 03:18:16
问题 I'm using Python 3.2's xml.etree.ElementTree , and am attempting to generate XML like this: <XnaContent xmlns:data="Model.Data"> <Asset Type="data:MyData"> ... The format is out of my control (it's XNA). Notice that the data XML namespace is never actually used to qualify elements or attributes, but rather to qualify attribute values to XNA. My code looks like this: root = Element('XnaContent') ET.register_namespace('data', 'Model.Data') asset = SubElement(root, 'Asset', {"Type": "data:MyData

Editing local XML file using Python and Regular expression

陌路散爱 提交于 2019-12-11 02:32:51
问题 I am new to python and trying to modify some xml configuration files which are present in my local system. Input: I have an xml file(say Test.xml) with the following content. <?xml version="1.0" encoding="UTF-8" standalone="no"?> <JavaHost xmlns="SomeInfo/v1.1"> <Domain> <MessageProcessor> <!-- This comment should not be removed and all formating should be untouched --> <SocketTimeout>500</SocketTimeout> </MessageProcessor> <!-- This comment should not be removed and all formating should be