Reputation: 47
I am a beginner in python 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
Upvotes: 2
Views: 1953
Reputation: 601889
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)
Upvotes: 1
Reputation: 76838
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.
Upvotes: 1