0

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?

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Mehdi
  • 1,146
  • 1
  • 14
  • 27

2 Answers2

2

Catch the exception and then treat it:

try:
    a = ds.SeriesDescription
except AttributeError:
   pass or something else
Jean Rostan
  • 1,056
  • 1
  • 8
  • 16
2

This is generally a good way to check for an attribute that might be missing:

if 'SeriesDescription' in ds:
   ds.SeriesDescription = None  # or whatever you would like

You can also do:

a = ds.get('SeriesDescription')

which will return None if that item does not exist, or

a = ds.get('SeriesDescription', "N/A")

if you want to set your own value if the attribute does not exist.

darcymason
  • 1,223
  • 6
  • 9