Reputation: 181
I need to match the values from two columns, but then output the value from a separate column that alines with only one of the original columns.
Here is an example of the spreadsheet I am trying to get info from:
Unique_ID, fullname, role, students_needed
1234, john doe, student, tim young
5678, steven tyler, student, john doe
9101, sarah joe, student, ashley fonz
2758, ashley fonz, student, rick rose
If I needed to figure out what students from 'student_needed' were in the 'fullname' column and then print out the 'Unique_ID' of those student, how would I do it?
Here is the code that I have so far that I am using from a different csv project, but I can't seem to change it to make it work. It keeps printing vertical, but it isn't printing the correct information either.
1 # 1. Match Student full name with CAMS Students
2 # 2. Match Match Schoology ID with CAMS Students
3 # 3. Match instructor ID with Schoology ID
4 # 4. create new csv file with student ID matched with instructor ID
5
6 import os
7 import csv
8 from itertools import groupby
9 from operator import itemgetter
10
11 path = "/Users/brandon/desktop/"
12
13 writers = {}
14
15 with open(os.path.join(path, "Schoology_Unique_IDs.csv"), "rbU") as rosters:
16
17 reader = csv.DictReader(rosters)
18 headers = reader.fieldnames
19
20 for row in reader:
21 if not all(row): continue
22 fullname = row['Full Name']
23 Schoology_ID = row['Unique User ID']
24 CAMS_Student = row['CAMS Students']
25 Teacher = row['Teacher Name']
26 for row in fullname:
27 for row in CAMS_Student:
28 if row == row:
29 print row
Upvotes: 0
Views: 1102
Reputation: 5391
Here is how I'd do it. Read the file once to get the proper name, id relationship in a dictionary, then do it again to match names with keys.
import csv
with open("Schoology_Unique_IDs.csv","rbU") as f1, open("Schoology_Unique_IDs.csv","rbU") as f2:
r1 = csv.reader(f1)
r1.next()
d = {r[1]:r[0] for r in r1}
r2 = csv.reader(f2)
print "\n".join([d[i[3]] for i in r2 if i[3] in d])
This prints:
1234
2758
Upvotes: 1