Search code examples
pythoncsvdicompydicom

KeyError due to missing tag when converting DCM to CSV


I would appreciate your help with this. I want to convert DCM to CSV here's the code that I try

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)

and result

if ds.data_element(field) is None:

    return self[tag]

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

so what should I do?

Thank you in advance for your help in this matter.


Solution

  • You're going to run into problems with a naive conversion because you may need to handle 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). However, if all you care about are element in the top-level of the dataset:

    # 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)