elementtree

xml filtering with python

本秂侑毒 提交于 2020-07-05 09:16:08
问题 I have a following xml document: <node0> <node1> <node2 a1="x1"> ... </node2> <node2 a1="x2"> ... </node2> <node2 a1="x1"> ... </node2> </node1> </node0> I want to filter out node2 when a1="x2" . The user provides the xpath and attribute values that need to tested and filtered out. I looked at some solutions in python like BeautifulSoup but they are too complicated and dont preserve the case of text. I want to keep the document same as before with some stuff filtered out. Can you recommend a

How can I insert an XML element between text in the parent element using ElementTree

人走茶凉 提交于 2020-06-26 07:28:18
问题 I want to generate XML like this: <Element>some text <Child>middle text</Child> some more text</Element>. How can I do this using ElementTree? I couldn't find it in the docs. I thought element#insert would work, but that's for inserting a child in a specific position relative to other children. 回答1: You need to define the child element and set it's .tail, then append it to the parent: import xml.etree.ElementTree as ET parent = ET.Element("Element") parent.text = "some text " child = ET

How can I insert an XML element between text in the parent element using ElementTree

元气小坏坏 提交于 2020-06-26 07:27:50
问题 I want to generate XML like this: <Element>some text <Child>middle text</Child> some more text</Element>. How can I do this using ElementTree? I couldn't find it in the docs. I thought element#insert would work, but that's for inserting a child in a specific position relative to other children. 回答1: You need to define the child element and set it's .tail, then append it to the parent: import xml.etree.ElementTree as ET parent = ET.Element("Element") parent.text = "some text " child = ET

ElementTree is not preserving namespaces although in debugging it shows it does

北城余情 提交于 2020-05-17 07:05:12
问题 I'm working with XML document, where I edit text and attributes of tags, however I encountered a problem. ElementTree namespace registration does not work properly. The process is that I parse XML document, strip namespaces, register them in order to preserve them as the are on the input, make changes in some tags and then save(write) the final document. The problem is that it does not read all of the namespaces and edit them after saving. When I debug script, it shows however (as I believe),

SyntaxError: prefix 'a' not found in prefix map

瘦欲@ 提交于 2020-05-15 03:37:26
问题 I'm trying to create a function which counts words in pptx document. The problem is that I can't figure out how to find only this kind of tags: <a:t>Some Text</a:t> When I try to: print xmlTree.findall('.//a:t') , it returns SyntaxError: prefix 'a' not found in prefix map Do you know what to do to make it work? This is the function: def get_pptx_word_count(filename): import xml.etree.ElementTree as ET import zipfile z = zipfile.ZipFile(filename) i=0 wordcount = 0 while True: i+=1 slidename =

Python - ElementTree- cannot use absolute path on element

青春壹個敷衍的年華 提交于 2020-05-10 03:41:47
问题 I'm getting this error in ElementTree when I try to run the code below: SyntaxError: cannot use absolute path on element My XML document looks like this: <Scripts> <Script> <StepList> <Step> <StepText> </StepText> <StepText> </StepText> </Step> </StepList> </Script> </Scripts> Code: import xml.etree.ElementTree as ET def search(): root = ET.parse(INPUT_FILE_PATH) for target in root.findall("//Script"): print target.attrib['name'] print target.findall("//StepText") I'm on Python 2.6 on Mac. Am

Add comment to beginning of document

自作多情 提交于 2020-04-30 10:27:44
问题 Using ElementTree, how do I place a comment just below the XML declaration and above the root element? I have tried root.append(comment) , but this places the comment as the last child of root . Can I append the comment to whatever is root 's parent? Thanks. 回答1: Here is how a comment can be added in the wanted position (after XML declaration, before root element) with lxml, using the addprevious() method. from lxml import etree root = etree.fromstring('<root><x>y</x></root>') comment = etree

How to traverse through XML document tags using lxml similarly to ElementTree

随声附和 提交于 2020-04-30 06:35:27
问题 Currently I'm editing XML document, where I have to edit few tags and their attributes. Up to now I was using ElementTree library, however I encountered problems with namespace preservation, so I'm trying to rewrite my script to use lxml . ElementTree however was very logical for me in case of traversing through the document tags. Below as an example, I'll provide code that will remove Ext tag in XML, and change Resolution tag text to different value. ElementTree: namespaces = dict([elem for

Repeating elements to new rows ElementTree

隐身守侯 提交于 2020-03-25 19:19:32
问题 The code below takes a directory of XMLs files and parses them into a CSV fie. This was possible only for a user in this community. I have learned so much. from xml.etree import ElementTree as ET from collections import defaultdict import csv from pathlib import Path directory = 'C:/Users/docs/FolderwithXMLs' with open('output.csv', 'w', newline='') as f: writer = csv.writer(f) headers = ['id', 'service_code', 'rational', 'qualify', 'description_num', 'description_txt', 'set_data_xin', 'set

Replace values in XML file with values of a vector [closed]

元气小坏坏 提交于 2020-03-25 18:40:42
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 26 days ago . I have an XML file and I need to replace the value that comes right after "type" with a value from a vector v=[7,8,9] using a Python code. I need the code to recognize the word "type" and then change the value of the parameter in the following line. The value 2 should be replaced by v[0] , 3 by v[1