从python的Excel中获取一个列到数组中

作为一个项目的一部分,我需要创build一个联赛表格,为了让它按点sorting,我需要从excel中访问点列并命令它。 到目前为止,我为此编写的代码是:

output = [] x = open("table.csv", "rU") for line in x: cells = line.split(",") output.append((cells[7])) print output 

积分是所有列中的最后一个,共有7个列。 这个输出是:

 ['Points\n', '0\n', '0\n', '0\n', '0\n', '0\n', '0\n', '0\n', '0\n', '0\n', '0\n'] 

有没有办法只得到数字,然后订购他们,而不使用pandas?

谢谢

你应该看看CSV模块 ,它会帮助你做到这一点。

看看你已经有的代码,你想跳过列标题,并在“单元格”上调用strip()来删除新行。 在输出之前,对输出列表进行sorting:

 output = [] with open("table.csv", "rU") as x: next(x) # skip header row for line in x: cells = line.split(",") output.append((cells[7].strip())) output.sort() print output 

你的代码可以简化为:

 with open("table.csv", "rU") as f: output = sorted([line.split(',')[7].strip() for line in f][1:]) 

或者你可以使用CSV模块:

 import csv with open("table.csv", "rU") as f: reader = csv.reader(f) next(reader) output = sorted(row[7] for row in reader)