简体   繁体   English

我不知道如何将数据写入到csv文件中以及如何正确执行排序代码

[英]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.

相关问题 我的代码中不断出现错误,无法弄清楚 - I keep getting an error in my code and can't figure out 我试图了解类和函数,但似乎无法弄清楚我的代码出了什么问题 - I am trying to understand class and function and can't seem to figure out what is wrong with my code 无法弄清楚为什么我的函数返回带有字谜的True - Can't figure out well why my function is returning True with anagrams 无法弄清楚如何使用all()在我的“代码”中工作 - Can't figure out how to use all() to work in my “code” 无法弄清楚如何正确输出我的数据 - Can't figure out how to properly output my data 我的Python代码没有从.csv excel文件中提取所有信息,而且我不知道为什么 - My Python code isn't pulling all the information from the .csv excel file and I cannot figure out why 尝试使用 Python 将解析的数据导出到 CSV 文件,但我不知道如何导出超过一行 - Attempting to export parsed data to CSV file with Python and I can't figure out how to export more than one row Python-我的部分代码不起作用,我不知道为什么 - Python- Parts of my code aren't working and I can't figure out why 我不知道为什么我的 web 抓取代码不起作用 - I can't figure out why my web scraping code isn't working 我无法弄清楚我的代码不起作用的原因 - I can't figure out the reasoning why my code doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM