how to read data from xml file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: how to read data from xml file (/thread-9510.html) |
how to read data from xml file - Raj - Apr-13-2018 I have excell file, I use below code: # -*- coding: utf-8 -*- """ Created on Fri Apr 13 20:33:17 2018 @author: user """ #from xml.dom import minidom # #doc = minidom.parse("D:\Mekala_Backupdata\PythonCodes\input.xml") import xml.etree.cElementTree as ET tree = ET.ElementTree('D:\Mekala_Backupdata\PythonCodes\input.xml') root = tree.getroot() for books in root: if (books.tag=='book'): print books.get('id') for attr in books: if (attr.tag==author'): print (attr.text)But it does not work can some help me, My xml file is
RE: how to read data from xml file - dwill - Apr-13-2018 Hi.. You have: if (attr.tag==author'):...but looks like you left off a single quote before author? should be: if (attr.tag=='author'): RE: how to read data from xml file - Larz60+ - Apr-13-2018 take a look at w3schoold XML tutorial here: The 'Books.xml' file looks almost identical: https://www.w3schools.com/xml/xml_usedfor.asp RE: how to read data from xml file - Raj - Apr-14-2018 It still give error, Below is my code: [python][python]import xml.etree.cElementTree as ET tree = ET.ElementTree('input.xml') root = tree.getroot() for books in root: if (books.tag=='book'): print books.get('id') % here it says invalid syntax for attr in books: if (attr.tag=='author'): print (attr.text)[/python][/python] RE: how to read data from xml file - dwill - Apr-14-2018 Hi I changed your code to the below. I used the "parse" method/function to parse the xml file into the "tree" object. Also, I put the source file in my temp folder..but you can change that back to the location you were running from. import xml.etree.cElementTree as ET source_file = 'C:\Temp\input.xml' tree = ET.parse(source_file) root = tree.getroot() for books in root: if (books.tag=='book'): print(books.get('id')) for attr in books: if (attr.tag=='author'): print (attr.text)The output I get is:
RE: how to read data from xml file - Raj - Apr-14-2018 # -*- coding: utf-8 -*- """ Created on Fri Apr 13 20:33:17 2018 @author: user """ #from xml.dom import minidom # #doc = minidom.parse("D:\Mekala_Backupdata\PythonCodes\input.xml") #import xml.etree.cElementTree as ET #tree = ET.ElementTree('D:\Mekala_Backupdata\PythonCodes\input.xml') #root = tree.getroot() #for books in root: # if (books.tag=='book'): # print books.get('id') # for attr in books: # if (attr.tag=='author'): # print (attr.text) import xml.etree.cElementTree as ET source_file = 'D:\Mekala_Backupdata\PythonCodes\input.xml' tree = ET.parse(source_file) root = tree.getroot() for books in root: if (books.tag=='book'): print(books.get('id')) for attr in books: if (attr.tag=='author' or attr.tag=='title' or attr.tag=='price'): print (attr.text)the code is workig, But in tilte part, I only want first part before ":" for example: in my above xml file the last title is: Visual Studio 7: A Comprehensive Guide I only want upto "Visual Studio 7" (or if I want to take the part after ":" i.e A Comprehensive Guide) kinldy help how to do this RE: how to read data from xml file - snippsat - Apr-14-2018 (Apr-14-2018, 10:31 AM)Raj Wrote: for example: in my above xml file the last title is:Split it up,can also make a dicionarey of it. Here also a alternative way with BeautifulSoup or lxml these are just better and more updated parser than parses in standard library. Also easier to use. from bs4 import BeautifulSoup soup = BeautifulSoup(open(r'C:\1_py\my.xml'), 'xml') book = soup.find('book', id="bk112") title = book.title.text print(title) # Make dictionray lst = title.split(':') d = dict([lst])Test: >>> Visual Studio 7: A Comprehensive Guide >>> d {'Visual Studio 7': ' A Comprehensive Guide'} >>> d['Visual Studio 7'] ' A Comprehensive Guide' >>> d.keys() dict_keys(['Visual Studio 7']) >>> d.values() dict_values([' A Comprehensive Guide']) RE: how to read data from xml file - Raj - Apr-14-2018 which command I should write after if (attr.tag=='author' or attr.tag=='title' or attr.tag=='price'): print (attr.text) Not just one tiltle, but in all titles, I wish only to take the first part of it (before :) |