1

So I understand how sorting works in Python. If I put...

a = (["alpha2A", "hotel2A", "bravo2C", "alpha2B", "tango3B", "alpha3A", "zulu.A1", "foxtrot8F", "zulu.B1"]
a.sort()
print a

I will get...

'alpha2A', 'alpha2B', 'alpha3A', 'bravo2C', 'foxtrot8F', 'hotel2A',  'tango3B', 'zulu.A1', 'zulu.B1']

However, I want to sort a column in a Excel sheet so I tried...

isv = open("case_name.csv", "w+")
a = (["case_name.csv"[2]])
a.sort()
print a

And got a return of...

['s']

I understand that it is returning the 3rd letter in the file name but how do I make it sort and return the entire column of the Excel sheet?

Update: New Code

import csv
import operator

with open('case_name.csv') as infile:
    data = list(csv.reader(infile, dialect=csv.excel_tab))

data.sort(key=operator.itemgetter(2))

with open('case_name_sorted.csv', 'w') as outfile:
    writer = csv.writer(outfile, dialect='excel')
    writer.writerows(data)

print(sum(1 for row in data if len(row) < 3))

And it returns

data = list(csv.reader(infile, dialect=csv.excel_tab))
_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
Community
  • 1
  • 1
Max Rogers
  • 115
  • 1
  • 8

1 Answers1

0
import csv
import oprator

# read the data from the source file
with open('case_name.csv') as infile:
    data = list(csv.reader(infile, dialect='excel'))

# sort a list of sublists by the item in index 2
data.sort(key=operator.itemgetter(2))


# time to write the results into a file
with open('case_name_sorted.csv', 'w') as outfile:
    writer = csv.writer(outfile, dialect='excel')
    writer.writerows(data)
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • data = list(csv.reader(infile, dialect='excel')) _csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode? – Max Rogers Jun 30 '15 at 18:45
  • Ack! that sounds like an issue with the excel dialect. As I don't usually work with excel files, I have never faced this issue before. However, I would recommend reading [this](http://stackoverflow.com/q/17315635/198633) – inspectorG4dget Jun 30 '15 at 18:48
  • ok I did that but now it says...IndexError: list index out of range. What line was I supposed to have changed? – Max Rogers Jun 30 '15 at 19:05
  • That error makes me think that you have at least one row that has fewer than 3 columns – inspectorG4dget Jun 30 '15 at 19:06
  • All of the rows have three columns in total and I am only trying to sort the last column, everything is filled in. There are no empty cells. – Max Rogers Jun 30 '15 at 19:08
  • What does `print(sum(1 for row in in data if len(row) < 3))` print? – inspectorG4dget Jun 30 '15 at 19:09
  • I put what my code looks like now as an answer, and the code you just told me to add does not print anything for some reason. – Max Rogers Jun 30 '15 at 19:26
  • Firstly, please edit your original post with the new code, etc. To address your problem, you haven't posted the error you get, or any output. I would recommend that you fix the indentation in your new code, and also use `infile` within the first `with` statement – inspectorG4dget Jun 30 '15 at 19:32