mardi 2 novembre 2021

Python prevent if inside if?

I am new to python and wrote:

root = ET.fromstring(xml_output)
host = root.find('host')
if host:
    ports = host.find('ports')
    if ports:
        for port in ports:
            service = port.find('service')
            if service:
                print(service.attrib.get('name'))

My code works find but it looks very ugly, how can I solve this? It's going too much deep that it's hard to read, plus what If I want to add other fields under name? that will be terrible code design.

Aucun commentaire:

Enregistrer un commentaire