My aim is to find the max of the individual column and print out the information. But there is problem when I print some of the information. For example CSIT135
, nothing was printed out. CSIT121
only prints out one result.
My data looks like:
first_name,last_name,student_id,CSIT110,CSIT121,CSIT135,CSIT142
Peter,Tan,S1012342D,89,67,54,78
John,Lim,S1014322H,87,78,86,67
Ada,Ang,S1023456I,54,78,65,54
def test():import csv with open("data.csv") as a: rows = csv.DictReader(a) t2_list=[]for row in rows: t2 = row['CSIT121']t2_list.append(t2)CSIT121=max(t2_list) if row['CSIT121']==CSIT121:print("{0:<8}| {1:>10} | {2:<8}".format("CSIT121", "John","Lim"))print("{0:<8}| {1:>10} | {2:<8}".format("CSIT121", row['first_name'],row['last_name']))with open("data.csv") as a: rows = csv.DictReader(a) t3_list=[]for row in rows: t3 = row['CSIT135']t3_list.append(t3)CSIT135=max(t3_list) if row['CSIT135']==CSIT135:print("{0:<8}| {1:>10} | {2:<8}".format("CSIT135", row['first_name'],row['last_name']))
Code sample and run result pic
You haven't specified the format of the output, so instead of printing the result I written function returning dict with each key representing each column and each value containing dict representing row with max value in that column.
I don't like the file rewinding part, but it seems to be necessary, because the csv.DictReader during iteration is using the file handle it has received in the constructor and doesn't rewind it after the iteration. This might be why you only see one result with your code.
import csvdef get_maxes():with open("data.csv", "r") as data_file:data = csv.DictReader(data_file)# don't process first 3 columscolumns_to_process = data.fieldnames[3:]column_max = {}for column in columns_to_process:data_file.seek(0) # rewind the file after iteration in line abovedata_file.readline() # skip the first line with headercolumn_max[column] = max(data, key=lambda x: x[column])return column_maxif __name__ == '__main__':print(get_maxes())
Output:
{'CSIT110': {'CSIT110': '89','CSIT121': '67','CSIT135': '54','CSIT142': '78','first_name': 'Peter','last_name': 'Tan','student_id': 'S1012342D'},'CSIT121': {'CSIT110': '87','CSIT121': '78','CSIT135': '86','CSIT142': '67','first_name': 'John','last_name': 'Lim','student_id': 'S1014322H'},'CSIT135': {'CSIT110': '87','CSIT121': '78','CSIT135': '86','CSIT142': '67','first_name': 'John','last_name': 'Lim','student_id': 'S1014322H'},'CSIT142': {'CSIT110': '89','CSIT121': '67','CSIT135': '54','CSIT142': '78','first_name': 'Peter','last_name': 'Tan','student_id': 'S1012342D'}}
EDIT:
If you consume all the rows at once from the DictReader you don't need to rewind the file:
import csvdef get_maxes():with open("data.csv", 'r') as data_file:data = csv.DictReader(data_file)columns_to_process = data.fieldnames[3:] # don't process first 3 columsdata = [row for row in data] # read all the data from DictReader and store it in the listcolumn_max = {}for column in columns_to_process:column_max[column] = max(data, key=lambda x: x[column])return column_maxif __name__ == '__main__':import pprintpprint.pprint(get_maxes())