在python中用NAN替换AttributeError

2024-09-29 19:18:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python中的以下代码从dicom头读取序列描述。你知道吗

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

但是,由于此特定图像的dicom标头中此部分为空,因此出现以下错误:

AttributeError: Dataset does not have attribute 'SeriesDescription'.    

如何防止此错误消息并将其替换为NAN?你知道吗


Tags: 代码图像read错误ds序列datasetfile
2条回答

这通常是检查可能缺少的属性的好方法:

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

您还可以执行以下操作:

a = ds.get('SeriesDescription')

如果该项不存在,则返回None,或者

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

如果属性不存在,则要设置自己的值。你知道吗

捕获异常,然后处理它:

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

相关问题 更多 >

    热门问题