CSV代码问题和帮助

所以我试图创build一个Python脚本,将sortingExcel工作表的指定列。 到目前为止,我的代码是…

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) 

但是,当我运行这个代码,我继续得到一个错误,说…

 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? 

我做了一些研究,发现.csv文件在Mac上无法正常工作。 那么我应该怎样改变这个文件才能像Excel工作表一样工作呢? 另外,如果任何人有什么指示我怎么可以sorting列,我会非常感激一些技巧。 谢谢!

尝试用该模式打开文件

 open('case_name.csv', mode='rU') 

比较: https : //docs.python.org/2/library/functions.html#open