【问题标题】:Replace AttributeError with NAN in python在python中用NAN替换AttributeError
【发布时间】:2018-04-10 15:29:44
【问题描述】:

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

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

但是,我收到以下错误,因为该部分在此特定图像的 dicom 标头中为空白:

AttributeError: Dataset does not have attribute 'SeriesDescription'.    

如何防止出现此错误消息并将其替换为 NAN?

【问题讨论】:

    标签: python python-2.7 pydicom


    【解决方案1】:

    捕获异常然后处理它:

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

    【讨论】:

      【解决方案2】:

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

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

      你也可以这样做:

      a = ds.get('SeriesDescription')
      

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

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

      如果你想在属性不存在的情况下设置你自己的值。

      【讨论】:

        猜你喜欢
        • 2019-04-22
        • 2019-11-08
        • 2023-03-04
        • 1970-01-01
        • 2018-04-18
        • 1970-01-01
        • 2019-05-05
        • 2021-09-16
        • 2019-03-16
        相关资源
        最近更新 更多