Python 如何在CSV中添加列

Python 如何在CSV中添加列,python,list,csv,row,Python,List,Csv,Row,所以上次我发布了我的问题。它被删除了。这是可以理解的。所以现在我回来了,我会专注于我的问题。 这是我的密码 grade = input("Enter grade file: ") lines = open(grade).read().splitlines() table = [x.split(",") for x in lines if x != ""] for row in table: row[1] = int(row[1]) def point_cal(x): if x =

所以上次我发布了我的问题。它被删除了。这是可以理解的。所以现在我回来了,我会专注于我的问题。 这是我的密码

grade = input("Enter grade file: ")
lines = open(grade).read().splitlines()
table = [x.split(",") for x in lines if x != ""]
for row in table:
    row[1] = int(row[1])
def point_cal(x):
    if x == "A":
        return float(4.0)
    elif x == "B+":
        return float(3.5)
    elif x == "B":
        return float(3.0)
    elif x == "C+":
        return float(2.5)
    elif x == "C":
        return float(2.0)
    elif x == "D+":
        return float(1.5)
    elif x == "D":
        return float(1.0)
    else:
        return float(0.0)
for row in table:
    print(f"subject: {row[0]} credits: {row[1]} grade: {row[2]} point: {point_cal(row[2])}")
total_credits = sum([row[1] for row in table])
print(f"Total credits: {total_credits}")
y = [point_cal(row[2])*row[1] for row in table]
GPA = sum(y)/total_credits
print(f"GPA = {GPA:.2f}")
这是我作业的结果

Enter grade file: 1.txt
subject: 01175165 credits: 1 grade: A point: 4.0
subject: 01204111 credits: 3 grade: B point: 3.0
subject: 01355111 credits: 3 grade: B+ point: 3.5
subject: 01355112 credits: 3 grade: C point: 2.0
subject: 01355113 credits: 3 grade: D+ point: 1.5
subject: 01417167 credits: 3 grade: F point: 0.0
subject: 01420111 credits: 3 grade: C+ point: 2.5
subject: 01420113 credits: 1 grade: C point: 2.0
subject: 01999021 credits: 3 grade: D point: 1.0
subject: 01999043 credits: 3 grade: A point: 4.0
subject: 01999111 credits: 2 grade: A point: 4.0
Total credits: 28
GPA = 2.38
你会发现这一行的点是错的。与另一个变量不同。我认为这是因为我没有在CSV文件中的新列中添加点

这是我的CSV文件(无法编辑)


所以我想知道如何将我的“点”列表添加到CSV文件中的新列中。所以它会一直向下。不像输出那样错误。谢谢

您能否编辑您的答案,以显示您想要实现的结果?我读了你的文章好几次,但我不明白。他想让专栏排得很整齐。问题似乎是,grade列有时是1个字符,有时是2个字符。
01175165,1,A
01204111,3,B

01355111,3,B+
01355112,3,C
01355113,3,D+

01417167,3,F
01420111,3,C+
01420113,1,C

01999021,3,D
01999043,3,A
01999111,2,A