Python pretty XML printer with lxml

For me, this issue was not solved until I noticed this little tidbit here: http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output Short version: Read in the file with this command: >>> parser = etree.XMLParser(remove_blank_text=True) >>> tree = etree.parse(filename, parser) That will “reset” the already existing indentation, allowing the output to generate it’s own indentation correctly. Then pretty_print as normal: >>> tree.write(<output_file_name>, … Read more

How to use lxml to find an element by text?

You are very close. Use text()= rather than @text (which indicates an attribute). e = root.xpath(‘.//a[text()=”TEXT A”]’) Or, if you know only that the text contains “TEXT A”, e = root.xpath(‘.//a[contains(text(),”TEXT A”)]’) Or, if you know only that text starts with “TEXT A”, e = root.xpath(‘.//a[starts-with(text(),”TEXT A”)]’) See the docs for more on the available … Read more

python lxml – modify attributes

import lxml.etree as et tree = et.fromstring(”’ … your xml … ”’) for host_ip in tree.xpath(“/scenario/init/send/command[@name=”CER”]/avp[@name=”Host-IP-Address”]”): host_ip.attrib[‘value’] = ‘foo’ print et.tostring(tree)

Building lxml for Python 2.7 on Windows

I bet you’re not using VS 2008 for this 🙂 There’s def find_vcvarsall(version): function (guess what, it looks for vcvarsall.bat) in distutils with the following comment At first it tries to find the productdir of VS 2008 in the registry. If that fails it falls back to the VS90COMNTOOLS env var. If you’re not using … Read more

How do you install lxml on OS X Leopard without using MacPorts or Fink?

Thanks to @jessenoller on Twitter I have an answer that fits my needs – you can compile lxml with static dependencies, hence avoiding messing with the libxml2 that ships with OS X. Here’s what worked for me: cd /tmp curl -O http://lxml.de/files/lxml-3.6.0.tgz tar -xzvf lxml-3.6.0.tgz cd lxml-3.6.0 python setup.py build –static-deps –libxml2-version=2.7.3 –libxslt-version=1.1.24 sudo python … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)