elementtree

Comment out and uncomment an xml element

最后都变了- 提交于 2019-12-08 13:17:27
I have an xml file and I want to uncomment and comment out an element in the file. <my_element> <blablabla href="docs/MyBlank.htm" /> </my_element> This one I would like to "close" (comment out) like this: <!-- <my_element> <blablabla href="docs/MyBlank.htm" /> </my_element> --> Furter down in the file I have an element with the same name which is "closed" (commented out) like this: <!-- <my_element> <blablabla href="secretwebhacking/MySecrectBankLogin.htm" /> </my_element> --> and I want to "open" it up (uncomment) like: <my_element> <blablabla href="secretwebhacking/MySecrectBankLogin.htm" /

Comment out and uncomment an xml element

余生长醉 提交于 2019-12-08 08:45:12
问题 I have an xml file and I want to uncomment and comment out an element in the file. <my_element> <blablabla href="docs/MyBlank.htm" /> </my_element> This one I would like to "close" (comment out) like this: <!-- <my_element> <blablabla href="docs/MyBlank.htm" /> </my_element> --> Furter down in the file I have an element with the same name which is "closed" (commented out) like this: <!-- <my_element> <blablabla href="secretwebhacking/MySecrectBankLogin.htm" /> </my_element> --> and I want to

Remove a node from etree but leaving child

筅森魡賤 提交于 2019-12-08 07:40:44
问题 Im traversing a XML tree and im having some troubles by extracting a node from the tree leaving their inner nodes. For example: <xml> <letter name="B"> <letter name="D"> <letter name="E"> <letter name="F"> <letter name="G"> </letter> </letter> </letter> </letter> </letter> </xml> I need something like this: <xml> <letter name="B"> <letter name="D"> <letter name="F"> <letter name="G"> </letter> </letter> </letter> </letter> </xml> But i cant get this with out removing all E childs. Cheers! 回答1

upper case html tags encoded in lxml

让人想犯罪 __ 提交于 2019-12-08 07:30:53
问题 I am parsing an html file using lxml.html....The html file contains tags with small case letters and also large case letters. A part of my code is shown below: response = urllib2.urlopen(link) html = response.read().decode('cp1251') content_html = etree.HTML(html_1) first_link_xpath = content_html.xpath('//TR') print (first_link_xpath) A small part of my HTML file is shown below: <TR> <TR vAlign="top" align="left"> <!--<TD><B onmouseover="tips.Display('Metadata_WEB', event)" onmouseout="tips

Beautiful Soup - `findAll` not capturing all tags in SVG (`ElementTree` does)

帅比萌擦擦* 提交于 2019-12-08 07:23:04
问题 I was attempting to generate a choropleth map by modifying an SVG map depicting all counties in the US. The basic approach is captured by Flowing Data. Since SVG is basically just XML, the approach leverages the BeautifulSoup parser. The thing is, the parser does not capture all path elements in the SVG file. The following captured only 149 paths (out of over 3000): #Open SVG file svg=open(shp_dir+'USA_Counties_with_FIPS_and_names.svg','r').read() #Parse SVG soup = BeautifulSoup(svg,

PYTHON : How to add root node to an XML

淺唱寂寞╮ 提交于 2019-12-08 06:36:01
问题 I have an xml file looks something like this <A> <B> <C> .... </C> </B> </A> I want to add root on top of element 'A'. I found out a way to add elements to root. But How to change existing root and add on top of it using python. After adding root to the xml it should look like this <ROOT> <A> <B> <C> .... </C> </B> </A> </ROOT> 回答1: import lxml.etree as ET tree = ET.parse('data') root = tree.getroot() newroot = ET.Element("root") newroot.insert(0, root) print(ET.tostring(newroot, pretty_print

Is there a way to add a comment on the first line of a XML file using ElementTree?

徘徊边缘 提交于 2019-12-08 05:45:37
问题 When generating a XML file using ElementTree, you can add a comment on the line below an element by using insert , e.g. import xml.etree.ElementTree as ET tree = ET.ElementTree("test.xml") # modifying existing XML file tree.getroot().insert(0, ET.Comment("Some comment...")) tree.write("test_modified.xml") This results in an output looking something like this: <RootElement> <!--Some comment...--><NextElement> </NextElement> </RootElement> Is there a way to add a comment on the first line

using fromstring() with lxml prefixes

一笑奈何 提交于 2019-12-08 05:35:58
问题 I have a variable ele. I'm trying to append a child node onto ele that contains a namespace prefix (called style) in its tag. ele seems to be aware of this prefix, as the line: print(ele.nsmap['style']) outputs urn:oasis:names:tc:opendocument:xmlns:style:1.0 But when I try to run ele.append(etree.fromstring('<style:style />')) I get the error lxml.etree.XMLSyntaxError: Namespace prefix style on style is not defined What am I missing here? 回答1: etree.fromstring('<style:style />') throws an

Running out of memory using python ElementTree

荒凉一梦 提交于 2019-12-08 04:16:08
问题 EDIT: Anyone coming to this in the future, the solution I used was to switch to cElementTree. It not only runs with less memory, it is significantly faster. This works on files up to about 600mb in size, larger than that and I run out of memory (I have a 16gb machine). What can I do to read in a file in pieces, or read in a certain percentage of the xml at a time or is there a less memory intensive approach? import csv import xml.etree.ElementTree as ET from lxml import etree import time

Change value in specific child element

青春壹個敷衍的年華 提交于 2019-12-08 03:28:31
问题 I have some issues with my script. First of i wanna make sure that the correct name has been given by the user. For example if I were to write "Name" it should not match with anything in my xml. If i were to write "NameY" it should match with given name and not anything else(for example "NameX should not be matched). I tried using wordboundry but it did not help as I thought it would. When I try want to change value in NameY the script changes for all the values(even for NameX). I believe