XML Parsing

Stefan Behnel stefan_ml at behnel.de
Mon Apr 6 01:54:41 EDT 2015


Sepideh Ghanavati schrieb am 06.04.2015 um 04:26:
> I know basic of python and I have an xml file created from csv which has
> three attributes "category", "definition" and "definition description".
> I want to parse through xml file and identify actors, constraints,
> principal from the text. However, I am not sure what is the best way to
> go. Any suggestion?

If it's really generated from a CSV file, you could also parse that instead:

https://docs.python.org/3/library/csv.html

Admittedly, CSV files are simple, but they also have major problems,
especially when it comes to detecting their character encoding and their
specific format (tab/comma/semicolon/space/whatever separated, with or
without escaping, quoted values, ...). Meaning, you can easily end up
reading nonsense from the file instead of the content that was originally
put into it.

So, if you want to parse from XML instead, use ElementTree:

https://docs.python.org/3/library/xml.etree.elementtree.html

Stefan




More information about the Python-list mailing list