How do I extract specific rows from a CSV file?

Question:

I have three CSV files:

doctors.csv

1,John,Smith,Internal Med
2,Jone,Smith,Pediatrics
3,Jone,Carlos,Cardiology

patients.csv

1,Sara,Smith,20,07012345678,B1234
2,Mike,Jones,37,07555551234,L22AB
3,Daivd,Smith,15,07123456789,C1ABC

… and linked.csv, which I need to populate based on doctors.csv and patients.csv.

I’m taking two inputs from the user that correspond with the doctor ID and patient ID, and checking if they are present, and then writing them to the linked.csv file.

I’d like the linked.csv file to contain for each column:

[patientID,patientfirstname,patientsurname,doctorID,doctorfirstname,doctorlastname]

Unfortunately, I can’t figure out how to read a specific row using the csv module and then extract the specific data I need from both files.

Here is the code I have so far:

#asking for input
print('Please select both a Patient ID and Doctor ID to  link together')
patient_index = input('Please enter the patient ID: ')
doctorlink = input('Please select a doctor ID: ')
doctorpresent = False
patientpresent = False

# precence check for both values
with open('patiens.csv', 'r') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            if patient_index == row[0]: 
                print('Patient is present')
                patientpresent = True
with open('doctors.csv', 'r') as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            if patient_index == row[0]: 
                print('Doctor is present')
                doctorpresent = True

if patientpresent == True and doctorpresent == True:            

Here, I need to add the code necessary for extracting the rows.

Asked By: G0rg81

||

Answers:

This looks like a task better suited for a database, but here is a possible solution just using the csv module (pathlib here is just my preferred way to handle files):

import csv
from pathlib import Path

# Files
patients_csv = Path('patients.csv')
doctors_csv = Path('doctors.csv')
linked_csv = Path('linked.csv')

# Create in memory lists for patients and doctors
with patients_csv.open() as f:
    reader = csv.reader(f)
    patients = [_ for _ in reader]

with doctors_csv.open() as f:
    reader = csv.reader(f)
    doctors = [_ for _ in reader]

print('Please select both a Patient ID and Doctor ID to  link together')
patient_id = input('Please enter the patient ID: ')
doctor_id = input('Please select a doctor ID: ')

# Is there a doctor that matches doctor_id?
doctor = [_ for _ in doctors if _[0] == doctor_id]

# Is there a patient that matches patient_id?
patient = [_ for _ in patients if _[0] == patient_id]

# Do we have a patient and a doctor match?
if patientt and doctor:
    patient = patient[0]
    doctor = doctor[0]
    with linked_csv.open('a') as f:
        print(*patient, *doctor, file=f, sep=',')

And here is the result of a test run of the code above, which is in app.py here:

$ ls
app.py  doctors.csv  patients.csv

$ cat doctors.csv 
1,John,Smith,Internal Med
2,Jone,Smith,Pediatrics
3,Jone,Carlos,Cardiology

$ cat patients.csv 
1,Sara,Smith,20,07012345678,B1234
2,Mike,Jones,37,07555551234,L22AB
3,Daivd,Smith,15,07123456789,C1ABC

$ python app.py 
Please select both a Patient ID and Doctor ID to  link together
Please enter the patient ID: 1
Please select a doctor ID: 1

$ cat linked.csv 
1,Sara,Smith,20,07012345678,B1234,1,John,Smith,Internal Med

It can also be implemented independent of the csv module:

from pathlib import Path

# Files
patients_csv = Path('patients.csv')
doctors_csv = Path('doctors.csv')
linked_csv = Path('linked.csv')

# Create in memory lists for patients and doctors
with patients_csv.open() as f:
    patients = [_.strip().split(',') for _ in f]

with doctors_csv.open() as f:
    doctors = [_.strip().split(',') for _ in f]

print('Please select both a Patient ID and Doctor ID to  link together')
patient_id = input('Please enter the patient ID: ')
doctor_id = input('Please select a doctor ID: ')

# Is there a doctor that matches doctor_id?
doctor = [_ for _ in doctors if _[0] == doctor_id]

# Is there a patient that matches patient_id?
patient = [_ for _ in patients if _[0] == patient_id]

# Do we have a patient and a doctor match?
if patientt and doctor:
    patient = patient[0]
    doctor = doctor[0]
    with linked_csv.open('a') as f:
        print(*patient, *doctor, file=f, sep=',')

Both examples are just starting points, which you can construct and improve above them.

Answered By: accdias
import csv

# asking for input
print('Please select both a Patient ID and Doctor ID to  link together')
patient_index = input('Please enter the patient ID: ')
doctor_index = input('Please select a doctor ID: ')

output = []

# opening the CSV file
with open('patient.csv', 'r') as f:
  # reading the CSV file
  reader = csv.reader(f, delimiter=',')
  # iterating the rows
  for row in reader:
    if patient_index == row[0]:
        output.append(row[0])  # appending patientID
        output.append(row[1])  # appending patientfirstname
        output.append(row[2])  # appending patientlastname

# opening the CSV file
with open('doctor.csv', 'r') as f:
  # reading the CSV file
  reader = csv.reader(f, delimiter=',')
  # iterating the rows
  for row in reader:
    if doctor_index == row[0]:
        output.append(row[0])  # appending doctorID
        output.append(row[1])  # appending doctorfirstname
        output.append(row[2])  # appending doctorlastname

print(output)

Output

['1', 'Sara', 'Smith', '1', 'John', 'Smith'] 
[patientID,patientfirstname,patientsurname,doctorID,doctorfirstname,doctorlastname]
Answered By: louis joseph
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.