如何在Excel工作表中对列进行sorting

所以我理解如何在Python中进行sorting。 如果我把…

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

我会得到…

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

但是,我想sorting一个Excel表中的一列,所以我试着…

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

并得到了回报…

 ['s'] 

我知道它是返回文件名中的第三个字母,但是如何使它sorting并返回Excel表单的整个列?

更新:新代码

 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)) 

然后它返回

 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? 

 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)