1

I am very new to python and am really struggling with this problem. I have a csv file with different columns, labeled "height" "weight" "full_name" etc. I'm trying to create a function that will look through the full_name column and return the longest name. (So if the longest name in the folder was Rachel Smith, I'm trying to return that value.)

Here the code that's worked the best so far:

import csv
file = "personal_data.csv"
f = open(file)
reader = csv.reader(f, delimiter=",")
col_index = next(reader).index('full_name')
highest = max(rec[col_index] for rec in reader)
print(highest) #using this statement to test if it works
f.close()

I think it's not working because it's only printing Rachel, not her full name, Rachel Smith. I'm not really sure though.

Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
Emppy
  • 15
  • 4

2 Answers2

1

You can try to use key= parameter in max() function:

import csv

with open("personal_data.csv", "r") as f_in:
    reader = csv.reader(f_in, delimiter=",")
    col_index = next(reader).index("full_name")

    highest = max([rec[col_index] for rec in reader], key=len)  # <-- use key=len here

print(highest)  # using this statement to test if it works
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • omg thank you! Would I add return highest somewhere? Or do I not have to do that and can just print it? – Emppy Dec 08 '22 at 19:25
  • @Emppy Well, you can make a function that accepts a filename as a parameter and returns the longest words. It depends on your program and what you want... – Andrej Kesely Dec 08 '22 at 19:30
  • for the code I have to return the longest name, is there a way I could just add a quick line of code to return it? – Emppy Dec 08 '22 at 19:41
0

Use csv.DictReader to eliminate the need to find the full_name column index. Use max()'s key argument to make it return the value rather than the length of the value.

import csv


with open('personal_data.csv', 'r') as csvfile:
    reader = csv.DictReader(csvfile)
    longest_name = max([row['full_name'] for row in reader], key=len)

print(longest_name)

If the file is large enough that you care about memory usage, use map() and itemgetter() to get the names and pass as the iterable argument to max().

import csv
import operator


with open('personal_data.csv', 'r') as csvfile:
    reader = csv.DictReader(csvfile)
    names = map(operator.itemgetter('full_name'), reader)
    longest_name = max(names, key=len)

print(longest_name)

Package into function:

import csv


def get_longest_value_from_col(filename, column_name):
    with open(filename, 'r') as csvfile:
        reader = csv.DictReader(csvfile)
        longest_name = max([row[column_name] for row in reader], key=len)

    return longest_name
Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
  • thank you!! do you know how I would change the print into a return function, or is that a different question I should look up? – Emppy Dec 08 '22 at 19:47
  • It's a different question, but "how do I define a function" isn't really appropriate for SO since it's very clearly explained in the language documentation. I edited the answer to show one way to convert the code to a function. – Michael Ruth Dec 08 '22 at 19:54
  • Thank you! I tried printing get_longest_value_from_col and it printed this: – Emppy Dec 08 '22 at 20:16
  • do you know what that means? – Emppy Dec 08 '22 at 20:17
  • Yes, it's how a function object is represented: . You're probably using the expression `get_longest_value_from_col` rather than `get_longest_value_from_col('personal_data.csv', 'full_name')`. A function must be called with an argument list. – Michael Ruth Dec 08 '22 at 20:21