Parsing a xml.gz file in python

11,857

Below Code worked for me, to read and process a zipped xml file.
I have used gzip first to unzip the file and then used ElementTree.

import gzip
import xml.etree.ElementTree as ET

input = gzip.open('input-xml.gz', 'r')
tree = ET.parse(input)
root = tree.getroot()

print root.tag
print root.attrib
Share:
11,857
Admin
Author by

Admin

Updated on June 13, 2022

Comments

  • Admin
    Admin almost 2 years

    I have an tar.gz file on my local machine called abc.aXML.gz, which contains many XML files. I want to find some data from these files but don't know how to parse these file using Elementtree and gzip.

    import xml.etree.ElementTree as ET
    import gzip
    document = ET.parse(gzip("abc.aXML.gz"))
    root = document.getroot()
    
  • anish
    anish about 5 years
    Changing the last line to dom = minidom.parseString(content) worked for me