将 DCM 转换为 CSV 时由于缺少标记而导致 KeyError

KeyError due to missing tag when converting DCM to CSV

非常感谢你的帮助。 我想将 DCM 转换为 CSV 这是我尝试的代码

PathDicom = "path image"
dicom_image_description = pd.read_csv("dicom_image_description.csv")
# Specify the .dcm folder path
folder_path = PathDicom
images_path = os.listdir(folder_path)
# Patient's information will be stored in working directory #'Patient_Detail.csv'
with open('Patient_Detail.csv', 'w', newline ='') as csvfile:
    fieldnames = list(dicom_image_description["Description"])
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerow(fieldnames)
    for n, image in enumerate(images_path):
        ds = dicom.dcmread(os.path.join(folder_path, image))
        rows = []
        for field in fieldnames:
            if ds.data_element(field) is None:
                rows.append('')
            else:
                x = str(ds.data_element(field)).replace("'", "")
                y = x.find(":")
                x = x[y+2:]
                rows.append(x)
        writer.writerow(rows)

和结果

if ds.data_element(field) is None:

    return self[tag]

    data_elem = self._dict[tag]
KeyError: (0008, 0064)

那我该怎么办?

在此先感谢您对此事的帮助。

您将 运行 陷入天真的转换问题,因为您可能需要处理 sequence elements (think of a dataset as a tree-like data structure) and raw data for elements with byte VRs like OB, OD, OF, OL, OW (see here)。但是,如果您只关心数据集顶层的元素:

# Make sure that `fieldnames` is a list of element tags
for tag in fieldnames:
    if tag not in ds:
        writer.writerow('')
        continue

    elem = ds[tag]
    # Parse elem however you wish, watch out for elements with a byte VR though!
    value = elem.value
    if isinstance(value, bytes):
        value = "Binary data of length {}".format(elem.length)
    row = "{}, {}, {}".format(elem.tag, elem.VR, value)
    writer.writerow(row)