Posted on 29th March 2024
Let us assume there is a data mans.csv
{'age': '10', 'height': '54', 'id': '1', 'weight': '120', 'name': 'raj'}
{'age': '30', 'height': '66', 'id': '2', 'weight': '120.6', 'name': 'jai'}
{'age': '15', 'height': '55', 'id': '3', 'weight': '110.0', 'name': 'veer'}
Now import CSV
import csv
input_file = csv.DictReader(open("mans.csv"))
maximum_age = None
oldest_man = None
for row in input_file:
age = int(row["age"])
if maximum_age == None or maximum_age < age:
maximum_age = age
oldest_man = row["name"]
if maximum_age != None:
print "The oldest man is %s, who is %d % (oldest_man, maximum_age)
else:
print "not there any man in the file"
STILL GOT QUERIES?