[lxml] Re: ElementTree function insert() to accept negative value.

2025-06-18 Thread Adrian Bool
Hi Schimon, You've been using Python's built-in ElementTree implementation (xml.etree.ElementTree). This mailing list, and Xavier's guidance, is for the "lxml" package . So, ensure you've got lxml available within your project (e.g. "pip install lxml") and then import with:

[lxml] Re: lxml children vs. descendant not working correctly (I think)

2023-07-02 Thread Adrian Bool
Hi, Curious, I tried this out, first creating some data in the file child.py: #!/usr/bin/env python3 import lxml.etree as ET t = ET.fromstring( """ This content is flagged as TODO. It gets a background colour (red, in this case) and the label TODO is rendered into the document margin

[lxml] Re: Adding HTML inside XML

2022-08-18 Thread Adrian Bool
Hi Karl, You're not parsing the context_string as XML or HTML; so lxml will be thinking its just some text that looks horribly like XML but is not XML and therefore needs to be escaped to be included within XML. The following: import lxml.etree as etree content_text = 'line oneline two' en_no

[lxml] Re: Turn three-line block into single?

2022-08-08 Thread Adrian Bool
Hi Gilles, I guess you're intending on using 'sort -u' on your data? An alternative would be to de-dup the data as XML instead of as text. Here is something to play with... For the input file: London Paris

[lxml] Re: tostring() returning possible invalid XML. Is this a bug?

2022-06-07 Thread Adrian Bool
Hi Brian & Charlie, I'm not the OP; but, FYI, i can see the same issue (on an Intel Mac): aid@orac tmp % ./tail.py Python : sys.version_info(major=3, minor=9, micro=13, releaselevel='final', serial=0) lxml.etree : (4, 9, 0, 0) libxml used : (2, 9, 14) libxml compil

[lxml] Re: [newbie] lxml adds before each end of line

2022-05-12 Thread Adrian Bool
> On 12 May 2022, at 10:26, Gilles wrote: > Had another issue with parse(): It doesn't like reading from a string, so > StringIO() is the way to go: > > """ > Traceback (most recent call last): > File "C:\myscript.py", line 170, in > tree = et.parse(content, parser) > File "src\lxml\e

[lxml] Re: Adding block of HTML?

2022-05-12 Thread Adrian Bool
Hi Gilles, > On 12 May 2022, at 13:08, Gilles wrote: > 1. Is there a way to tell lxml _not_ to add and > when inserting the header right after ? You're not loading the header data with the HTMLParser are you?? It is the HTMLParser that adds etc. Just parse the content without a specific

[lxml] Re: Adding block of HTML?

2022-05-12 Thread Adrian Bool
More XML fun in the morning! #!/usr/bin/env python3 from lxml import etree as et html_parser = et.HTMLParser(encoding='utf8') WRAPPER_FILENAME = 'wrapper.html' CONTENT_FILENAME = 'content.html' wrapper_tree = et.parse(WRAPPER_FILENAME, parser=html_parser) # Note, we can't use HTML parser for

[lxml] Re: [newbie] lxml adds before each end of line

2022-05-11 Thread Adrian Bool
Hi Gilles > On 11 May 2022, at 14:03, Gilles wrote: > Tuens out there's no need to use the pathlib module: The issue with " " > is gone when 1) first reading HTML into a variable 2) before parsing it, even > with the standard open(): Sure, I just tend to use pathlib for all my file handling a

[lxml] Re: lxml memory allocation failed

2022-05-01 Thread Adrian Bool
Hi Martin, > On 30 Apr 2022, at 21:35, Martin Mueller > wrote: > >for elem in elem: > sort_and_indent(elem, level + 1) I'd never write the above code — i.e. using the same variable name for the for loop's iterator as it's input. The ambiguity of what the "elem" variable

[lxml] Re: [newbie] Different ways to find elements

2021-09-04 Thread Adrian Bool
Hi, My understanding is... xpath() The xpath function allows for complex queries based upon the XPATH standard. For example, from the following XML: Fred 34 Chris 22 Sarah 28 The following xpath query would pick out ju