Parsing XML files with Python (xml.etree.ElementTree)

  Рет қаралды 72,688

Francesco Cento

Francesco Cento

Күн бұрын

Overview of the Python xlm.etree.ElementTree module for parsing and editing and creating XML files.
Reference: docs.python.org/3/library/xml...
Next video of the series covering a special cases such as elements with missing tags: • Fixing Missing Element...

Пікірлер: 45
@jdvelasquezr
@jdvelasquezr Ай бұрын
Thank you, Francesco, for taking the time to review this library's different functions. You have greatly helped me finish a much-needed script for our localization engineering tasks. Notably, adding text to an existing tag saved the day.
@bayrakmusti1
@bayrakmusti1 Жыл бұрын
That's how it is supposed to be taught. I have been browsing the courses on how to do it and they all are complicated. Thankfully found this video. Thanks a lot. Great job!
@user-xu8od8tf9l
@user-xu8od8tf9l 9 ай бұрын
Thanks a lot for the great tutorial. Your approach to XML parsing was spot-on for me and it was exactly what I was looking for to get started on XML parsing.
@ginopeduto4264
@ginopeduto4264 3 жыл бұрын
Grazie Mille!!! That was exactly what I was looking for and all well explained!!!
@UsmanSaadat
@UsmanSaadat 2 жыл бұрын
Thanks a lot for this video. I couldn't grasp the concepts properly even after reading from books. This video made it look like piece of cake.
@RodrigoMontes
@RodrigoMontes Жыл бұрын
Excellent man! This is what I was looking for :)
@A_A7337
@A_A7337 2 жыл бұрын
Great video. Thanks
@stanleymbah8983
@stanleymbah8983 2 жыл бұрын
thank you for this
@giacomocillari4448
@giacomocillari4448 2 жыл бұрын
Is there a way to change sub-element instead of the whole element string? let's say for example that I want to change W with SW but not the name, and I need to do it in a loop so I can't put the name string inside as it changes anytime, is there a way to call the specific sub element?
@RS-el7iu
@RS-el7iu 3 жыл бұрын
thanks a lot ❤
@attilioturco
@attilioturco 5 ай бұрын
nice vid thanks
@debasishsahoo1268
@debasishsahoo1268 24 күн бұрын
Awesome
@KiviliG
@KiviliG 3 жыл бұрын
Can this be done by Beautifulsoup library?
@arnolda7417
@arnolda7417 3 жыл бұрын
Hi Francesco. Thanks for the great video! I ran into an error after editing my xml file. I tried to view the entire file to make sure my changes were made with ET.dump(tree) and I always get "AttributeError: 'str' object has no attribute 'items'" I'm testing with Jupyter notebook and when I restart the kernel, ET.dump works just fine before I make changes to the file. Any idea on how to fix this? I'm new to Python.
@fcento
@fcento 3 жыл бұрын
Hi Arnold, can you share the code?
@arnolda7417
@arnolda7417 3 жыл бұрын
@@fcento absolutely. Is there an email address I can send it to? I’d like to include the payload as well for reference
@hoscoharding7319
@hoscoharding7319 3 жыл бұрын
Hi Francesco! I have been trying to do something with elementtree for several days but it is impossible for me ... And it gives me the feeling that it is very simple. I want to make a little script that adds a child element only if it doesn't already have it. Imagine that the document lacks year to panama. My script would go through the xml document and add only the year to Panama ... Could you give me some idea please? Many thanks.
@fcento
@fcento 3 жыл бұрын
I've just made a video about it: kzfaq.info/get/bejne/a6iiibOhtM_KpKM.html
@xst9880
@xst9880 6 ай бұрын
Can you show us how to parse a Tableau dashboard file (*.twb)? It's an XML file, Tableau just renamed it. I am trying to create a data dictionary from the .twb file.
@vijayalakshmi8282
@vijayalakshmi8282 2 жыл бұрын
hii franseco great video thanks i need small suggestion here let's saya 100 so in this i need output like KTOPL 100 here i need tag and value both how we can get can u please explian
@sidjjj
@sidjjj 2 жыл бұрын
Thanks for this video, I needed to parse xml from a variable instead of a file and found this : xml_data_tree = ET.fromstring(received_packet)
@markdillon9588
@markdillon9588 2 жыл бұрын
can you mass edit multiple files?
@shrinivasulunandyala9269
@shrinivasulunandyala9269 2 жыл бұрын
Merge XML files using python,can you please make video on this top
@myyoutubeaccount0123_
@myyoutubeaccount0123_ 2 жыл бұрын
thanks a lot
@fcento
@fcento 2 жыл бұрын
Happy to help
@CinemagicMindset
@CinemagicMindset 2 жыл бұрын
Hi Francesco, i'm getting error while parsing xml file since it is having special words. kindly hep me to avoid this error. Error : xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 277, column 366
@fcento
@fcento 2 жыл бұрын
If you are sure the file you have is a valid xml (there are online tools to help you there), then what comes to mind is incorrect encoding. Check the documentation here: docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.XMLParser
@fantasticprajwal7442
@fantasticprajwal7442 3 жыл бұрын
How to install xmltree in python 2.7.5 I am not able to upgrade due to restriction
@fcento
@fcento 3 жыл бұрын
docs.python.org/2.7/library/xml.etree.elementtree.html it's a built in library no need to install. I recommend you start to figure out the restrictions because 2.7 is deprecated. 3.8 is now also available with Anaconda. Possibly some of the code I used will not work on 2.7, bear that in mind.
@KrishnaManohar8021
@KrishnaManohar8021 3 жыл бұрын
looking forword...
@arshap9351
@arshap9351 3 жыл бұрын
Increase your font size before doing tutorials. its quite complicated to read texts. anyway goodjob
@padraigmaccu9333
@padraigmaccu9333 3 жыл бұрын
Go raibh céad maith agat, a Francesco. Rud a bhí de dhíth orm le fada. Pádraig Mac Con Uladh
@codelearnexe475
@codelearnexe475 2 жыл бұрын
Was not expecting irish in this chat XD
@Gamer-mg6my
@Gamer-mg6my 2 жыл бұрын
Hi i'm trying to get the text of every tag named , but inside every tag has this: , some idea to extract/ the content of the tags?: 1 1 1 0 #000000 1 0.010000 #000000 #000000 1 #000000 0.000000 0.590551 0.000000 0.000000 0 0.000000 0 0.000000 1 #000000 0 1.000000 0.166667 -1  0 -1.200000 1.651575 0.748031 0 0 0 0.708661 3.720472 6.023622 1.612205 #000000 #FFFFFF 1 #000000 1 #000000 1 0.039370 0 0 0 0 0.000000 0.000000 1.612205 0.000000 1.612205 -0.708661 0.000000 -0.708661 0.000000 0.000000 0 0 0 0.247563 3.889961 5.511811 1.273228 #000000 0 1.000000 0.247563 1 Entity 0 0 0 0.708661 3.720472 6.023622 1.612205 #000000 #FFFFFF 1 #000000 1 #000000 1 0.039370 0 0 0 0 0.000000 0.000000 1.612205 0.000000 1.612205 -0.708661 0.000000 -0.708661 0.000000 0.000000 0 0 0 0.247563 3.889961 5.511811 1.273228 #000000 0 1.000000 0.247563 1 EntityTwo 0 0 0 0.708661 3.720472 6.023622 1.612205 #000000 #FFFFFF 1 #000000 1 #000000 1 0.039370 0 0 0 0 0.000000 0.000000 1.612205 0.000000 1.612205 -0.708661 0.000000 -0.708661 0.000000 0.000000 0 0 0 0.247563 3.889961 5.511811 1.273228 #000000 0 1.000000 0.247563 1 EntityThree
@fcento
@fcento 2 жыл бұрын
Let's take it in steps. I'm assuming you want to extract 'Entity', 'EntityTwo', 'EntityThree' from the element (...let me know if i misunderstood your question). The way it's formatted it contains 2 elements ( and ) as well as the piece of text you want to extract. If you just use findall() and use 'text' you get None back, what you want to use in this case is 'tail' instead. I've included a sample code here: gist.github.com/fcento100/74b8691af014a8126f8e9ca2ff03c6ea
@fcento
@fcento 2 жыл бұрын
i've put the xml code from your comment in a file here gist.github.com/fcento100/19cb7ae6b857c539a2c2843519239efc for convenience
@Gamer-mg6my
@Gamer-mg6my 2 жыл бұрын
@@fcento Yes, you understood me good. Ohhhh with tail .Well, i checked it but with other xml didn't compile :( , instead of that i put findall('.//cp', ns) and print elm.tail, with that we got the text. I like more your solution but with other xml didn't compile :(((((.This is the error that i got: elmtail = elm.tail.strip() AttributeError: 'NoneType' object has no attribute 'strip'
@fcento
@fcento 2 жыл бұрын
Apologies for not catching the 'NoneType' error, effectively 'tail' returns None if it doesn't find anything rather than an empty string. It's fixed now in this version: gist.github.com/fcento100/11847ad0d8d42eec6c1dc42de897b842 with an if statement to catch it. The reason i wasn't getting this error was because i copied pasted from your message and since it was formatted, 'tail' returned ' ' and '\t' (which are the string representation of new-line and tab) where it should have returned None, hence why i was able to run the strip command everywhere without error. In the new code i posted I've shown 2 methods of getting at that piece of data; in your sample xml "Entity" etc.. is the tail of ; root.findall('.//visio:Text/',ns) and root.findall('.//visio:cp',ns) do similar things. The only difference is that using './/visio:Text/' in method 1 will also extract the tail for if is available, which may be undesirable! In that case './/visio:cp' like you suggested is the way to go.
@Gamer-mg6my
@Gamer-mg6my 2 жыл бұрын
@@fcento a lot of thanks for your kind help Francesco :))
XML & ElementTree  ||  Python Tutorial  ||  Learn Python Programming
10:30
Parse XML Files with Python - Basics in 10 Minutes
10:07
Max on Tech
Рет қаралды 30 М.
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 20 МЛН
Did you believe it was real? #tiktok
00:25
Анастасия Тарасова
Рет қаралды 52 МЛН
1❤️
00:17
Nonomen ノノメン
Рет қаралды 13 МЛН
Parsing XML with Namespaces with Python (xml.etree.ElementTree)
16:56
Francesco Cento
Рет қаралды 19 М.
Python Intermediate Tutorial #10 - XML Processing
27:23
NeuralNine
Рет қаралды 35 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 372 М.
Makefiles in Python For Professional Automation
13:43
NeuralNine
Рет қаралды 40 М.
Automate your job with Python
6:07
John Watson Rooney
Рет қаралды 377 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 274 М.
Python XML Jumpstart in only 5 minutes
5:33
Michael Kennedy
Рет қаралды 121 М.
The Easiest Way to Parse XML with JavaScript
7:15
dcode
Рет қаралды 36 М.
Full XML Processing Guide in Python
17:34
NeuralNine
Рет қаралды 73 М.
How Many Balloons Does It Take To Fly?
00:18
MrBeast
Рет қаралды 20 МЛН