Search code examples
pythonpython-2.7pydicom

Replace AttributeError with NAN in python


I am using the following code in python to read the series description from the dicom header.

ds = dicom.read_file(mydcmfile.dcm)
a=ds.SeriesDescription

However, I get the following error because this part is blank in the dicom header for this specific image:

AttributeError: Dataset does not have attribute 'SeriesDescription'.    

How can I prevent this error message and replace it with NAN?


Solution

  • Catch the exception and then treat it:

    try:
        a = ds.SeriesDescription
    except AttributeError:
       pass or something else