Read the xml file by referring to the Python tutorial


    <country name="Liechtenstein">
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    <country name="Singapore">
        <neighbor name="Malaysia" direction="N"/>
    <country name="Panama">
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>

findall Refers to all the same child nodes

Specify the tag you want with find or findall Below you get all country tags

from xml.etree import ElementTree

path = 'data3.xml'
tree = ElementTree.parse(path)
root = tree.getroot()
countries = root.findall('country')
for country in countries:
    gdppc = country.find('gdppc')
{'name': 'Liechtenstein'}
{'name': 'Singapore'}
{'name': 'Panama'}

Get up to grandchild node

Nest for statements.

cnt = 1
for child in root:
    print('---', cnt, '---')
    print(child.tag, child.attrib, child.text)
    for gild in child:
        print(gild.tag, gild.attrib, gild.text)
    cnt += 1
--- 1 ---
country {'name': 'Liechtenstein'}

rank {} 1
year {} 2008
gdppc {} 141100
neighbor {'name': 'Austria', 'direction': 'E'} None
neighbor {'name': 'Switzerland', 'direction': 'W'} None
--- 2 ---
country {'name': 'Singapore'}

rank {} 4
year {} 2011
gdppc {} 59900
neighbor {'name': 'Malaysia', 'direction': 'N'} None
--- 3 ---
country {'name': 'Panama'}

rank {} 68
year {} 2011
gdppc {} 13600
neighbor {'name': 'Costa Rica', 'direction': 'W'} None
neighbor {'name': 'Colombia', 'direction': 'E'} None

Reference URL

Recommended Posts

Read the xml file by referring to the Python tutorial
How to switch the configuration file to be read by Python
Read the file line by line in Python
Read the file line by line in Python
Template of python script to read the contents of the file
Let's read the RINEX file with Python ①
Read the file by specifying the character code.
[Python] Read the specified line in the file
[Python Kivy] How to get the file path by dragging and dropping
[Implementation example] Read the file line by line with Cython (Python) from the last line
How to read a CSV file with Python 2/3
[Python] How to read excel file with pandas
How to erase the characters output by Python
Read line by line from a file with Python
Read Python csv file
Various ways to read the last line of a csv file in Python
Bucket / file operations (transfer / acquire / delete / read, etc.) to GCS by python Summary
Fourier transform the wav file read by Python, reverse transform it, and write it again.
[Python] Try to read the cool answer to the FizzBuzz problem
I tried to touch the CSV file with Python
[Python] You can save an object to a file by using the pickle module.
Download the file in Python
Read Namespace-specified XML in Python
[Python] Open the csv file in the folder specified by pandas
[python] Change the image file name to a serial number
[Python] Read the csv file and display the figure with matplotlib
Debug by attaching to the Python process of the SSH destination
How to read an Excel file (.xlsx) with Pandas [Python]
I tried to divide the file into folders with Python
Try to decipher the garbled attachment file name with Python
How to read text by standard input or file name specification like cat in Python
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Leave the troublesome processing to Python
Extract the xz file with python
[Python] Write to csv file with Python
Create a shell script to run the python file multiple times
Read the standard output of a subprocess line by line in Python
In the python command python points to python3.8
How to read the SNLI dataset
How to get the Python version
After calling the Shell file on Python, convert CSV to Parquet.
[Python] How to import the library
[pepper] Pass the JSON data obtained by python request to the tablet.
Have python read the command output
Sort the file names obtained by Python glob in numerical order
Read a file in Python with a relative path from the program
Read the old Gakushin DC application Word file (.doc) from Python and try to operate it.
Deploy the Django tutorial to IIS ①
Extract the targz file using python
Try to implement and understand the segment tree step by step (python)
[Python] Read the Flask source code
File upload to Azure Storage (Python)
[Python] Change the alphabet to numbers
[Python Tutorial] An Easy Introduction to Python
A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function
I tried to open the latest data of the Excel file managed by date in the folder with Python
Upload & download wav file to X server (X-Server) by FTP with Python
A memo organized by renaming the file names in the folder with python
If you are told cannot by Python import, review the file name
[Python] How to read a csv file (read_csv method of pandas module)
The file name was bad in Python and I was addicted to import