[Python] How to avoid AttributeError in multiple labels

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have dicom files from different sources and sometimes i get error like this:


    AttributeError: 'FileDataset' object has no attribute 'PatientAge' because some files doesn't have this attribute.
    ​

    I've created function to check objects for errors:

    def check_if_exists(self, object_attr):
    try:
    return str(object_attr)
    except:
    return 'NULL'


    But I get error in setText line before executing code from my function:

    self.label.setText(self.check_if_exists(ds.PatientAge))


    I have several labels like this so I wanted to create one function and check if each attribute exists but I'm not sure how to do this. Sometimes I use ds.PatientName, ds.PatientAge and sometimes ds[0x0022,0x7031].value. Using try except: or hasattr() function for each label is redundant and i'm trying to get the best solution.

    Can you give me some ideas?

    Continue reading...

Compartilhe esta Página