0

I have three .csv files and i would like python to sort the data in them. i have this code but when i launch it an error occurs. An error occurs in this line sort = sorted(csv1, key= operator.itemgetter(1)). The number at the end suppose to choose the column but it doesn't. I had this program on mac and it worked but when i launched it on windows it stopped working.

import operator
import csv

decision= input("How would you like to view information? Type score or surname.")
decision1= input("What calss would you like to see? Type class1 or class2 or class3.")


if (decision == 'score' and decision1 == 'class1'):
    sample = open('class1.csv', 'r')
    csv1 = csv.reader(sample,delimiter = ',')
    sort = sorted(csv1, key= operator.itemgetter(0))
    for eachline in sort:
        print (eachline)

if (decision == 'surname' and decision1 == 'class1'):
    sample = open('class1.csv', 'r')
    csv1 = csv.reader(sample,delimiter = ',')
    sort = sorted(csv1, key= operator.itemgetter(1))
    for eachline in sort:
        print (eachline)

if (decision == 'score' and decision1 == 'class2'):
    sample = open('class2.csv', 'r')
    csv1 = csv.reader(sample,delimiter = ',')
    sort = sorted(csv1, key= operator.itemgetter(0))
    for eachline in sort:
        print (eachline)

if (decision == 'surname' and decision1 == 'class2'):
    sample = open('class2.csv', 'r')
    csv1 = csv.reader(sample,delimiter = ',')
    sort = sorted(csv1, key= operator.itemgetter(1))
    for eachline in sort:
        print (eachline)

if (decision == 'score' and decision1 == 'class3'):
    sample = open('class3.csv', 'r')
    csv1 = csv.reader(sample,delimiter = ',')
    sort = sorted(csv1, key= operator.itemgetter(0))
    for eachline in sort:
        print (eachline)

if (decision == 'surname' and decision1 == 'class3'):
    sample = open('class3.csv', 'r')
    csv1 = csv.reader(sample,delimiter = ',')
    sort = sorted(csv1, key= operator.itemgetter(1))
    for eachline in sort:
        print (eachline)

2 Answers 2

2

Something like this?

import operator
import csv

sorter = raw_input("Set sort method (score or surname): ")
classNumber = raw_input("Set class number (1, 2 or 3): ")

def print_sorted(sortMethod, classNumber):
    classFile = 'class' + str(classNumber) + '.csv'
    with open(classFile, 'r') as fh:
        csvData = csv.DictReader(fh)
        sortedCsvData = sorted(csvData, key=operator.itemgetter(sortMethod))
        for line in sortedCsvData:
            print line

print_sorted(sorter, classNumber)

You would want to add some sort of validation of the input set by the user before running the function though.

This won't error out if there is a value missing for a column.

Edit to say: If you are using python 3 then you need to change raw_input() above to be input()

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

4 Comments

It still gives me an error Traceback (most recent call last): File "H:/Computing/sorting.py", line 2, in <module> import csv File "H:/Computing\csv.py", line 13, in <module> in_txt = csv.reader(open(txt_newfile, "rb"), delimiter = '\t') AttributeError: 'module' object has no attribute 'reader'
Did you use the code above? I'm not using csv.reader it's using csv.DictReader, so that error doesn't make sense. Why are you specifying a delimeter='\t' if in the original example you used a comma? I don't think that's the same thing you're running.
Sorry, fixed up a syntax error as well, so copy again.
Any luck trying this out?
1

It sounds like at least one of your rows is missing a value for column 1. csv.reader(sample) returns a generator of lists, where each list corresponds to a row, and the length of each list corresponds to the number of comma-separated values in the given row. So a file with data like this:

   John,Smith
   Joe
   Jane,Doe

Would give you (["John", "Smith"], ["Joe"], ["Jane", "Doe"]) when parsed by csv.reader.

The sorted function is breaking when operator.itemgetter tries to access an index (column) from one of the lists (rows) that doesn't exist. If you were sorting on column 1 in the above example, your code would break because there is only 1 element in the list ["Joe"]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.