从csv文件

时间:2017-11-10 17:42:00

标签: python python-3.x

我知道我之前问过这个,但是当我尝试测试这个功能时,我仍然不确定为什么会出现这个错误。有人可以帮我解决这个问题吗?

new_list.append(new_list [i] + num_list [i])    builtins.TypeError:+:'int'和'str'

的不支持的操作数类型

这是文件:

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的总和,然后找到平均值。与其他作业相同。

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.
class_avg -> [assignment_1_class_avg, assignment_2_class_avg...]
[62.666666666666664, 56.0, 59.333333333333336, 66.0]
'''
new_list = []
count = 0
for line in open_file:
    num_list = line.split(',')[4:]
    for i in range(len(num_list)):
        new_list.append(count)
        new_list.append(new_list[i] + num_list[i])
        count +=1
        avg = sum(float(new_list))/len(new_list)
return new_list

1 个答案:

答案 0 :(得分:1)

如注释中所述Patrick Artner,您将一个int和一个字符串一起添加,从而导致错误。使用pandas库进行CSV读取对于以下情况非常有用:

import pandas as pd

def class_avg(file_path):
    df = pd.read_csv(file_path)

    not_grades = 4  # Number of attributes that are not grades
    total_attributes = len(df.columns)
    avg = []

    for i in range(not_grades, total_attributes):
        avg.append(df.iloc[:, i].mean(axis=0))  # Get the column by index and calculate its mean

    return avg

此代码完全符合您的要求。您需要说明在成绩之前有多少属性,因为它们不是唯一具有数字的属性(否则它将计算学生的数字的平均值)。