简体   繁体   English

无法将从DICOM文件提取的详细信息写入CSV文件

[英]Can't write details extracted from DICOM file to csv file

I am not able to write the details extracted from a DICOM file to a CSV file. 我无法将从DICOM文件提取的详细信息写入CSV文件。 Here's the code which I have used - 这是我使用的代码-

import pydicom
import os
import pandas as pd
import csv 
import glob 

data_dir= 'C:\\Users\\dmgop\\Personal\\TE Project - Pneumonia\\stage_1_test_images_dicom' 
patients= os.listdir(data_dir)
myFile= open('patientdata.csv','w')
for image in patients:
    lung = pydicom.dcmread(os.path.join(data_dir, image))
    print (lung)
    writer = csv.writer(myFile)
    writer.writerows(lung)
    break

The error which is coming up is as follows - 即将出现的错误如下-

Traceback (most recent call last): File "C:\\Users\\dmgop\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\pydicom-1.2.0rc1-py3.6.egg\\pydicom\\dataelem.py", 追溯(最近一次通话):文件“ C:\\ Users \\ dmgop \\ AppData \\ Local \\ Programs \\ Python \\ Python36 \\ lib \\ site-packages \\ pydicom-1.2.0rc1-py3.6.egg \\ pydicom \\ dataelem.py ”
line 344, in getitem getitem中的第344行
return self.value[key] TypeError: 'PersonName3' object does not support 返回self.value [key] TypeError:“ PersonName3”对象不支持
indexing 索引

During handling of the above exception, another exception occurred: 在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last): File "C:\\Users\\dmgop\\Personal\\TE 追溯(最近一次通话):文件“ C:\\ Users \\ dmgop \\ Personal \\ TE
Project - Pneumonia\\detail_extraction.py", line 14, in 项目-Pneumonia \\ detail_extraction.py“,第14行,
writer.writerows(lung) File writer.writerows(肺)文件
"C:\\Users\\dmgop\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\pydicom-1.2.0rc1-py3.6.egg\\pydicom\\dataelem.py", “C:\\用户\\ dmgop \\应用程序数据\\本地\\程序\\ Python的\\ Python36 \\ LIB \\站点包\\ pydicom-1.2.0rc1-py3.6.egg \\ pydicom \\ dataelem.py”
line 346, in getitem 346行,在getitem中
raise TypeError("DataElement value is unscriptable " TypeError: DataElement value is unscriptable (not a Sequence) 引发TypeError(“ DataElement值不可写” TypeError:DataElement值不可写(不是序列)

Assuming the "break" statement in your for loop means you only want the info of the first image, try: 假设您的for循环中的“ break”语句意味着您只需要第一张图像的信息,请尝试:

import pydicom
import os
import csv 

data_dir = 'C:\\Users\\dmgop\\Personal\\TE Project-Pneumonia\\stage_1_test_images_dicom' 
patients = os.listdir(data_dir)
with open('file.csv','w') as myfile:
    writer = csv.writer(myFile)
    # patients[0] means get the first filename, no need for the for loop
    lung = pydicom.dcmread(os.path.join(data_dir, patients[0]))
    print(lung.formatted_lines)
    # pay attention to the function_call --> formatted_lines()
    writer.writerows(lung.formatted_lines())

Have a look at the Pydicom docs for FileDataset which is the return type for the dcmread method. 看看FileDatasetPydicom文档,它是dcmread方法的返回类型。
Should you want to write the data for all files in the directory, try the following: 如果要写入目录中所有文件的数据,请尝试以下操作:

import pydicom
import os
import csv 

data_dir = 'C:\\Users\\dmgop\\Personal\\TE Project-Pneumonia\\stage_1_test_images_dicom' 
patients = os.listdir(data_dir)
with open('file.csv','w') as myfile:
    writer = csv.writer(myfile)
    for patient in patients:
        if patient.lower().endswith('.dcm'):
            lung = pd.dcmread(os.path.join(data_dir, patient))
            writer.writerows(lung.formatted_lines())

Also have a look at the last part of this paragraph on the use of 'with open() as' 还可以看看本段的最后部分有关“ with open()as”的用法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM