Python 将文件中的行更改为列

Python 将文件中的行更改为列,python,python-3.x,Python,Python 3.x,有人知道我如何从这个文件中交替分配任务吗 Here is the file, grades.csv and the format. Last Name,First Name,Student No.,uTORid,A1,A2,A3,A4 Smith,Joe,9911991199,smithjoe9,99,88,77,66 Ash,Wood,9912334456,ashwood,11,22,33,44 Full,Kare,9913243567

有人知道我如何从这个文件中交替分配任务吗

Here is the file, grades.csv and the format.

       Last Name,First Name,Student No.,uTORid,A1,A2,A3,A4
       Smith,Joe,9911991199,smithjoe9,99,88,77,66
       Ash,Wood,9912334456,ashwood,11,22,33,44
       Full,Kare,9913243567,fullkare,78,58,68,88
我想得到这样的结果,然后加上所有作业的总值,然后除以每个作业的作业数,得到平均值

       [99, 11, 78]
       [88, 22, 58]
       [77, 33, 68]
       [66, 44, 88]

def class_avg(open_file):
'''(file) -> list of float
Return a list of assignment averages for the entire class given the open
class file. The returned list should contain assignment averages in the 
order listed in the given file.  For example, if there are 3 assignments 
per student, the returned list should 3 floats representing the 3 averages.
'''

      new_list = []
for line in open_file:
    grades_list = line.split(',')
    marks1 = grades_list[4:5]
    marks2 = grades_list[6:7]

    for item in range(min(marks1, marks2)):
        added_marks = marks1[item] + marks2[item]

这就是我到目前为止所做的,不太确定如何继续枚举,在这种情况下迭代是你的朋友。通过内置函数,求平均值很简单

def class_avg(open_file):
    '''(file) -> list of float
    Return a list of assignment averages for the entire class given the open
    class file. The returned list should contain assignment averages in the 
    order listed in the given file.  For example, if there are 3 assignments 
    per student, the returned list should 3 floats representing the 3 averages.
    '''
    marks=[[],[],[],[]]

    for line in open_file:
        grades_list = line.strip().split(',')
        for idx,i in enumerate(range(4,8)):
            marks[idx].append(int(grades_list[i]))    
    avgs = [] 

    for mark in marks:
        avgs.append(float(sum(mark)/(len(mark))))
    return(avgs)

昨天,我问了一个类似的问题

def class_avg(open_file):
'''(file) -> list of float
Return a list of assignment averages for the entire class given the open
class file. The returned list should contain assignment averages in the
order listed in the given file.  For example, if there are 3 assignments
per student, the returned list should 3 floats representing the 3 averages.
'''
marks = None
avgs = []
for line in open_file:
    grades_list = line.strip().split(',')
    if marks is None:
        marks = []
        for i in range(len(grades_list) -4):
            marks.append([])
    for idx,i in enumerate(range(4,len(grades_list))):
        marks[idx].append(int(grades_list[i]))
for mark in marks:
    avgs.append(float(sum(mark)/(len(mark))))
return avgs

根本不清楚你想做什么。请包括预期输出(请参阅)。请向我们展示您迄今为止所做的尝试,以便我们能够帮助您。请看一看。可能是另一个重复:看起来你每天都在问同样的问题,在过去的三天中,它表示在尝试测试时出现零除错误。我更改了函数,假设您已经打开了文件,并且您已经使用诸如
next(file)
file.readline()
之类的内容迭代了第一行。你没有理由在我的机器上运行时,用你给定的输入接收错误。。。如果你编辑你的帖子来包含剩下的代码,我可以帮你调试我的意思是这一行的平均值。append(float(sum(mark)/(len(mark)))有一个被零除的错误出于某种原因是的,我知道这就是你的意思,但是上面代码的长度
mark
应该总是3,所以不应该有零除错误,这意味着您必须对传递到函数中的文件执行了某些操作,从而导致错误。因此,如果你不发布其余的相关代码,特别是你如何打开文件以及在文件通过之前做了什么,我就无能为力了。我只想将作业分数设置为[99,11,78][88,22,58][77,33,68][66,44,88],我建议你等到有足够的代表发表评论。