0

My code should output a column of the csv file called name in alphabetical order. It comes up with list index out of range.

filename = class_name + ".csv"
csv.register_dialect('pipes', delimiter='|')

with open(filename, 'a',newline='') as fp:
    a = csv.writer(fp, delimiter=',')
    data=(name,score)              
    a.writerow(data)


if get_bool_input("Do you wish to view previous results for your class"):
    with open(filename, 'r') as f:
        reader = csv.reader(f, dialect = 'pipes')
        sortedlist = sorted(reader, key=operator.itemgetter(3), reverse=True)
        for row in reader:
            print (row)
else:
    input ("Press any key to exit")
4
  • Do all the rows of your input file have at least 4 columns of data in them? Commented Mar 9, 2016 at 18:41
  • yes but why(do they need 4 rows) Commented Mar 9, 2016 at 18:47
  • If you add the input file or at least a sample including beginning and end we might be able to help Commented Mar 9, 2016 at 19:26
  • They need to have at least four in order for the operator.itemgetter(3) to work...otherwise an error like you reported can occur. What line is the error occurring on? Commented Mar 9, 2016 at 20:46

1 Answer 1

2

The code below will open the file, read up the header determine the index of the "Name" column, extract all the names and return a sorted list of the names.

The data file looks like this:

Name    Score
Liam    95
Mason   87
Jacob   67
William 86
Ethan   75
Michael 94
Alexander   84
James   88
Daniel  64
Olivia  100
Sophia  86
Isabella    100
Ava 82
Mia 91
Emily   99
Abigail 82
Madison 91
Charlotte   99

Here's the code:

import csv
filename = 'soc153.csv'
def get_names(filename):
    in_list = []
    with open(filename, 'r') as f:
        reader = csv.reader(f)
        header_line = next(reader)
        name_column = header_line.index('Name')
        for row in reader:
            in_list.append(row[name_column])
    return(sorted(in_list))
if input("Do you wish to view previous results for your class?") == 'y':
    name_list = get_names(filename)

This produces the following values in name_list:

['Abigail', 'Alexander', 'Ava', 'Charlotte', 'Daniel', 'Emily', 'Ethan', 'Isabella', 'Jacob', 'James', 'Liam', 'Madison', 'Mason', 'Mia', 'Michael', 'Olivia', 'Sophia', 'William']

Sign up to request clarification or add additional context in comments.

8 Comments

[in_list.append(row[name_column]) for i in reader] looks incorrect since there's no variable named row defined by the for in the list comprehension (or anywhere else).
Good catch. Fixed. Thanks.
Good idea to test code before posting it. BTW, many consider using a throw-away list comprehension jst because it's one line to avoid writing two line for loop a poor practice because it wastes resources building an unneeded list.
The code was tested, actually; I decided to change "i" to "row" and the edit didn't take. Should have checked. I take your point w.r.t. throwaway list comp. I've modified the code. Thanks for your input.
Your welcome (and a good sport). FWIW, note that you can write a non-list comprehension loop as one line. i.e. for row in reader: in_list.append(row[name_column]) if you really want to. PEP 8 - The Style Guide for Python Code has only this to say about it: "While sometimes it's okay to put an if/for/while with a small body on the same line, never do this for multi-clause statements".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.