python CSV,找到最大值并打印信息 [英] python CSV , find max and print the information
问题描述
我的目的是找到单个列的最大值并打印出信息.但是当我打印一些信息时会出现问题.例如CSIT135
,没有打印任何内容. CSIT121
仅打印一个结果.
我的数据如下:
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:
名字,姓氏,学生编号,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
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']))
推荐答案
您尚未指定输出的格式,因此,我没有打印结果,而是编写了返回dict的函数,该函数返回表示每个列的每个键以及每个包含dict的值用该列中的最大值表示行.
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.
我不喜欢文件倒带部分,但是这似乎是有必要的,因为在迭代过程中,csv.DictReader使用的是构造函数中接收到的文件句柄,并且在迭代之后不倒带它. 这可能就是为什么您的代码只看到一个结果的原因.
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 csv
def get_maxes():
with open("data.csv", "r") as data_file:
data = csv.DictReader(data_file)
# don't process first 3 colums
columns_to_process = data.fieldnames[3:]
column_max = {}
for column in columns_to_process:
data_file.seek(0) # rewind the file after iteration in line above
data_file.readline() # skip the first line with header
column_max[column] = max(data, key=lambda x: x[column])
return column_max
if __name__ == '__main__':
print(get_maxes())
输出:
{'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'}}
如果您一次使用DictReader中的所有行,则不需要后退文件:
If you consume all the rows at once from the DictReader you don't need to rewind the file:
import csv
def 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 colums
data = [row for row in data] # read all the data from DictReader and store it in the list
column_max = {}
for column in columns_to_process:
column_max[column] = max(data, key=lambda x: x[column])
return column_max
if __name__ == '__main__':
import pprint
pprint.pprint(get_maxes())
这篇关于python CSV,找到最大值并打印信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!