Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中用NAN替换AttributeError_Python_Python 2.7_Pydicom - Fatal编程技术网

在python中用NAN替换AttributeError

在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

我使用python中的以下代码从dicom头读取系列描述

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")
如果属性不存在,则要设置自己的值