def rename_temp():
img_PATH = r"./test"
flag = "(1)"
csvfile = open(r'./patientID.csv', 'w', newline='')
for path, dirs, files in os.walk(img_PATH):
for filename in files: # 遍历所有文件
num = ((filename.split("_")[1]).split(")")[0]).split("(")[-1]
file_path = os.path.join(path, filename)
ds = pydicom.dcmread(file_path, force=True) # 读取dcm
patient_id = str(ds.PatientID)
patient_sex = str(ds.PatientSex)
patient_age = str(ds.PatientAge)
if patient_id != flag:
print(num)
print("{}***{}***{}".format(patient_id, patient_sex, patient_age))
writer = csv.writer(csvfile)
writer.writerow([num, patient_id,patient_sex,patient_age])
flag = patient_id
else:
pass
csvfile.close()
作者:QianLingjun