开发者

How to open a CSV,read a particular header of column and get the highest value

I am a beginner in pytho开发者_如何学Gon and I want to know How to open a CSV,read a particular header of column and get the highest value.The value should be stored in a variab

regards, sid


You can use the CSV-module for reading the file. To get the highest value, you will have to read the entire file and and always remember the highest value seen so far.


Use the csv module to parse the file. Calling next(reader) for a csv.Reader object will yield the first line as a tuple. You can use the index() method of tuples to find the index of the column name you are looking for. Finally, using max() with a generator expression extracts the highest value of the column you are looking for:

import csv
col_name = "name"
with open("file.csv", "rb") as f:
    reader = csv.reader(f)
    col_index = next(reader).index(col_name)
    highest = max(rec[col_index] for rec in reader)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜