[英]I can't figure out how to write to my data to a csv file as well as having my sorting code function correctly
if class_number == ('1') and sort_by == ('a') or ('A'):
csv_file = open('Class1_Test_Score.csv', 'a')
csv_file.write('\n')
csv_file.write(sname + ' ' + fname)
csv_file.write(',')
csv_file.write(score)
csv_file.close()
sort1 = open('Class1_Test_Score.csv', 'r')
sorting = csv.reader(sort1, delimiter = ',')
sort = sorted(sorting,key=operator.itemgetter(1))
for eachline in sort:
csv_file.write(eachline)
Is every row in your Nested List its own list object with length greater than 0 ? 嵌套列表中的每一行是否都是其自己的列表对象,其长度大于0?
The majority of your rows are likely list objects with multiple items, giving it a : 您的大多数行都可能是包含多个项目的列表对象,并给它一个:
len ( row ) > 1
But I'm guessing there may be at least one Row in your Nested List that has a length of 0. In other words whereas most of your rows look like this : 但是我想您的嵌套列表中可能至少有一个行的长度为0。换句话说,您的大多数行看起来像这样:
[ 'First Item' , 2 , "3rd Item" ]
There may be one Row that looks like this : 可能有一个Row看起来像这样:
[ ]
And you cannot sort with operator.itemgetter unless every row in that nested list has an item at index number 0 ( or another column number you choose to input in your itemgetter ). 而且,除非该嵌套列表中的每一行都有一个索引编号为0的项目(或您选择在itemgetter中选择的另一个列号),否则您不能使用operator.itemgetter进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.