# Save patients ID in a list from Database with Python

**URL:** <https://discourse.slicer.org/t/save-patients-id-in-a-list-from-database-with-python/30872>\
**Category:** Support\
**Created:** [July 29, 2023, 5:16pm UTC](https://discourse.slicer.org/t/save-patients-id-in-a-list-from-database-with-python/30872 "2023-07-29T17:16:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![LisaAlborghetti](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lisaalborghetti/32/67006_2.png) [@LisaAlborghetti](https://discourse.slicer.org/u/LisaAlborghetti)\
**Post date:** [July 29, 2023, 5:16pm UTC](https://discourse.slicer.org/t/save-patients-id-in-a-list-from-database-with-python/30872/1 "2023-07-29T17:16:15Z")

</div>

Hi everyone,  
I’m quite a new entry in the Slicer world. I’m trying to use a python script for batch conversion of structures to .vtk but I face some problems. The first issue I would like to solve is this: I loaded two test patients files and it seems they are correcly imported in the Database, but then when I use slicer.dicomDatabase.patients() I get a list of integers instead of patients IDs and subsequently I get some errors, trying to call the patients in other parts of the code. Here is a simple example:

```auto
from DICOMLib import DICOMUtils

input_folder = '/Users/lisaalborghetti/Desktop/Patients/dicom'

DICOMUtils.openTemporaryDatabase()

DICOMUtils.importDicom(input_folder)

all_patients = slicer.dicomDatabase.patients()

print("Patient list: ", all_patients)

patient_names = {}

# Loop through the patient IDs and extract patient names

for patient_id in all_patients:
  patient_name = slicer.dicomDatabase.fileValue(patient_id, '0010,0010')
  patient_names[patient_id] = patient_name

for patient_id, patient_name in patient_names.items():
  print(f"Patient ID: {patient_id}, Patient Name: {patient_name}")

```

This is what I see in 3Dslicer:

 ![Screenshot 2023-07-29 at 19.10.10](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/8/386be3c1eff8a17b55a527958773ece04f4f934c.png)

And this the terminal output:  
Patient list: (‘1’, ‘2’)  
Could not load “1”  
DCMTK says: No such file or directory  
File 1 could not be initialized.  
Could not load “2”  
DCMTK says: No such file or directory  
File 2 could not be initialized.  
Patient ID: 1, Patient Name:  
Patient ID: 2, Patient Name:

Am I doing something wrong?  
Thank you!

Lisa

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 29, 2023, 5:55pm UTC](https://discourse.slicer.org/t/save-patients-id-in-a-list-from-database-with-python/30872/2 "2023-07-29T17:55:48Z")

</div>

Hi -

Since `PatientID` is assigned by the institution and they are not globally unique, Slicer assigns these integer local ids that are unique within the database.

The `slicer.dicomDatabase.fileValue` takes a path to a dicom instance file as an argument, hence the error message and the result of `None`.

To get from a Patient, who may have multiple studies, each of which with multiple series, and each of those with multiple instances, you can drill down with logic like in this example:

[https://slicer.readthedocs.io/en/latest/developer\_guide/script\_repository.html#iterate-through-dicom-series](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#iterate-through-dicom-series)

---

<div class="post-metadata">

**Author:** ![LisaAlborghetti](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lisaalborghetti/32/67006_2.png) [@LisaAlborghetti](https://discourse.slicer.org/u/LisaAlborghetti)\
**Post date:** [July 31, 2023, 1:37pm UTC](https://discourse.slicer.org/t/save-patients-id-in-a-list-from-database-with-python/30872/3 "2023-07-31T13:37:24Z")

</div>

Thanks a lot Steve, that was helpful!
