HomeНаука и техникаRelated VideosMore From: Omar Quimbaya

Reading and Writing XML with Python 3.6.1

517 ratings | 44536 views
A short tutorial on how to read and write XML using Python. This will work for any version of Python later than Python 2.7. This will also work on any operating system, but the tutorial was done on Windows. Here is the Github page for more information: https://github.com/WritingPanda/Python-XML-Tutorial Thanks for watching!
Html code for embedding videos on your blog
Text Comments (48)
zhomart aripbai (3 days ago)
thanks for the video, very helpful, but I have one question. Can I save the result of XMLPARSE to put it in PostgreSQL?
Daniel Xavier (1 month ago)
Pretty good actually, that's a very good tutorial, you should do more
uniquepatel4253 (1 month ago)
@Omar Quimbaya I have liked the video. There are some concerns though. If you have noticed, it deleted your comments when you edited that same file with a script. Plus, if a Subelement has more than one attributes, writing to that original file re-arranges all the attributes alphabetically. Do you have any solutions for these issues? I am trying to change texts of attributes without changing their order or anything else.
Pranav Patel (2 months ago)
the os.path.join is not working for my case. Any reasons?
DaggerCR (2 months ago)
how to literally print all the xml. I mean, print <productListing title="Python Products"> <product id="1"> <name>Python Hoodie</name> <description>This hoodie will keep your code warm and fuzzy.</description> <cost>$49.95</cost> <shipping>$4.00</shipping> </product> <product id="2"> <name>Python T-Shirt</name> <description>This shirt will make you look cool while you code.</description> <cost>$19.95</cost> <shipping>$4.00</shipping> </product>...
DaggerCR (2 months ago)
forget it. I just did: f = open('products.xml', 'r') file_contents = f.read() print (file_contents) f.close()
Daz (2 months ago)
Excellent !! short & sweet
SamCropper (2 months ago)
Really clear and helpful. Thanks a lot Omar!
SD world (3 months ago)
Hi Omar, Can you help to explain, here how the element Child was created as Dictionary
Omar Quimbaya (2 months ago)
When the XML library is used in Python, it takes the XML and converts it to a dictionary for use within Python. It shifts the data to a format that makes it easier to work with in Python.
JIHUN KIM (3 months ago)
this is one of the best "python xml tutorial"!
korandobot (3 months ago)
when ever i try to create base_path the ide tells me (__file__) is not defined, not sure why
Avila Kumari (4 months ago)
good one. please do upload more videos.
Nicachipal (4 months ago)
Why am I getting this message when i do exactly what you did : "OSError at / [Errno 63] File name too long: b'<?xml version=\'1.0\' encoding=\'UTF-8\'?> ...... " follow with the XML structure. ? The only different step i did was pointing my base_path to an external link (http://some-xml-file.xml). Like this: url = 'http://some-xml-file.xml' base_path = requests.get(url) tree = ET.parse(r.content) root = tree.getroot() for child in root: print(child.tag) What am I doing wrong. I am using Django and trying to parse an XML file from external server. I want to display specific elements on a template (home - page), but it's not working. Any help would be appreciated. Thank you !
AbyssWalker240 • (5 months ago)
so how does one read attributes? (sorry I may have missed it)
Harry Padarri (4 months ago)
@AbyssWalker240 • ElementTree is an object which has built-in methods. You find a description of the methods in the help page for the class. Just use help() on an ElementTree object - for example like help(et.parse(path_to_your_file)) or on an Element object help(tree.getroot()). You could use the methods find or findall to get child nodes with a specific tag.  The method findtext is even more straight-forward.
AbyssWalker240 • (5 months ago)
like I wanna get the value of a tag with a certain attribute.
Seyed Mansourbeigi (5 months ago)
Could make some multi threading and multi processing, GUI and API and socket programming and network programming in mighty PYTHON.
shirsendu sarkar (5 months ago)
very nice tutorial
Mochamad Wahyu Hidayat (6 months ago)
I've been looking for an easy way to get the values in XML and found this tutorial. It's very helpful and understandable. I've tried it and it works well. Once again thanks!
Hemanth Surapaneni (6 months ago)
Omar quimbaya! Is it possible to convert python script which is written using PyQt4 or tkinter and convert that into a xml file? I am working on project for developing GUI which is having buttons when we click on it, it opens up like tree structure.
Omar Quimbaya (2 months ago)
Have you tried it out yet? Let us know when you do!
Sokhib Tukhtaev (6 months ago)
at 14:21, python adds all the info to xml file without **tabs** Is it possible to add tabs by coding and make it human readable?
Thyag Sundaramoorthy (6 months ago)
This is one of the tutorials where the instructor didn’t miss a single sentence and also didn’t add extra wasteful sentences. It’s perfect and to the point. A model tutorial for others to follow. Should the instructor make any Udemy programming courses, I would queue up to buy.
Thyag Sundaramoorthy (4 months ago)
Omar Quimbaya please do!
Omar Quimbaya (4 months ago)
That is high praise. I need to make more videos, then!
Ashfaque Sayed (6 months ago)
Nice explanation.
Sheriff S (7 months ago)
Ive looked through dozen tutorials, but only with your help ive made my first read-write parsing script.Thanks!
mr. dotchetter (7 months ago)
Awesome stuff! Helped me out moving along with the write-to-xml-part of an algorithm I'm working on.. comparing text file data with contents of a pretty massive XML tree. Now I need to figure out how to 'prettify' the output...
Leonardo Pacheco (8 months ago)
Great tutorial!!! Thanks, it helped me a lot!!!
Jake Huneycutt (9 months ago)
Great tutorial. I'll probably figure out before anyone answers, but just in case ... my question --- in the video you add a new item (Python Pants), but what if we wanted to edit an existing item (e.g. Python Baseball Cap and turn it into Python Pants)?
Omar Quimbaya (2 months ago)
That is definitely possible!
Om Chaudhary (1 year ago)
this is really helpful to Me thanks
Brady Huang (1 year ago)
It's rare to find a Python XML related tutorial, for the sake of people like me find your tutorial and I am really grateful.
Claude Cordemans (1 year ago)
Thanks for this very clear tutorial. A true luck for a newbie to find such a gem !!! Sunny greetings from Belgium :-)
iMPRE7ed (1 year ago)
Everything's now clear - thanks so much, was really easy to follow
sina jahadi (1 year ago)
Very nice tutorial! Thank you
Kumbam Saikrishna (1 year ago)
Fantastic Explanation but please post how to create using frameworks
Prachi Saxena (1 year ago)
Fantastic explanation
Bertrahm (1 year ago)
Thank You, very good tutorial!
Rosa Poon (1 year ago)
Python xmltree
Lincoln Karim (1 year ago)
thanks for the knowledge...can't believe you only received 50 likes from almost 5000 views.. People making dog videos are getting millions of views and as many likes. Perhaps there'll be a turnaround one day. Thanks again.
Omar Quimbaya (1 year ago)
I have one dislike, too. I am okay with it, though. lol
novicetech1 (1 year ago)
Awesome tutorial. Very clear explanations. Easy to follow along. I'm a complete newbie and this really helps. Thank you.
Ahmed Hussain (1 year ago)
where can i buy python pants
Thunderjaw (1 year ago)
Thank you!
veggiet2009 (1 year ago)
Thank you! Even though I know some of this stuff, I've only used it in particular circumstances, it's help to hear a very clear general description of the process of using XML data in Python!
Charlie B (1 year ago)
Great tutorial! Thanks for posting.

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.