在python中用NAN替换AttributeError
我使用python中的以下代码从dicom头读取系列描述在python中用NAN替换AttributeError,python,python-2.7,pydicom,Python,Python 2.7,Pydicom,我使用python中的以下代码从dicom头读取系列描述 ds = dicom.read_file(mydcmfile.dcm) a=ds.SeriesDescription 但是,由于此特定图像的dicom标题中此部分为空,因此出现以下错误: AttributeError: Dataset does not have attribute 'SeriesDescription'. 如何防止此错误消息并将其替换为NAN?捕获异常,然后处理它: try: a = ds.Serie
ds = dicom.read_file(mydcmfile.dcm)
a=ds.SeriesDescription
但是,由于此特定图像的dicom标题中此部分为空,因此出现以下错误:
AttributeError: Dataset does not have attribute 'SeriesDescription'.
如何防止此错误消息并将其替换为NAN?捕获异常,然后处理它:
try:
a = ds.SeriesDescription
except AttributeError:
pass or something else
捕获异常,然后处理它:
try:
a = ds.SeriesDescription
except AttributeError:
pass or something else
这通常是检查可能缺少的属性的好方法:
if 'SeriesDescription' in ds:
ds.SeriesDescription = None # or whatever you would like
您还可以执行以下操作:
a = ds.get('SeriesDescription')
如果该项不存在,将返回None,或者
a = ds.get('SeriesDescription', "N/A")
如果要在属性不存在时设置自己的值。这通常是检查可能缺少的属性的好方法:
if 'SeriesDescription' in ds:
ds.SeriesDescription = None # or whatever you would like
您还可以执行以下操作:
a = ds.get('SeriesDescription')
如果该项不存在,将返回None,或者
a = ds.get('SeriesDescription', "N/A")
如果属性不存在,则要设置自己的值