繁体   English   中英

无法将数据写入 csv 文件

[英]Not able to write data into csv file

我无法将数据写入 csv 文件。 我能够分离 header 并添加新列,还能够添加总分,但似乎它不起作用

源文件

name,Homework_1,Homework_2
Naveed,20,19
Mahvish,10,18

目标文件

name,Homework_1,Homework_2,total
Mahvish,10,18,28.0
Naveed,20,19,39
import csv


def letterGrade(score):
    if score >= 90:
        letter = 'A'
    elif score >= 80:
        letter = 'B'
    elif score >= 70:
       letter = 'C'
    elif score >= 60:
        letter = 'D'
    else:
        letter = 'F'  #fall through or default case
    return letter

with open('marks.csv','r') as r:
    csvReader=csv.reader(r)
 
    header=next(csvReader)
    header.append("total")
    
   
    total=0
    if header!=None:
        for row in csvReader:
            total=0
            for index in range(1,3):
                thisGrade = row[index]
                if thisGrade == '':
                    thisGrade = 0.0  # change a nothing to a zero
                else:
                    thisGrade = float(thisGrade) 
                total = total + thisGrade
            percent = (total  * 100.)/ 200.  # out of a possible 200 points

            gradeToReport = letterGrade(percent)
        
            row.append(total)
            print(row)
            
            
            with open('marks_op.csv','w',newline='') as w:
                
                csvWriter=csv.writer(w,delimiter=',')
                
                csvWriter.writerow(i for i in header)
                #csvWriter.writerows(row)
                csvWriter.writerow(row)
            

您在每次循环迭代中覆盖 output 文件

with open('marks_op.csv','w',newline='') as w:

应该

with open('marks_op.csv','a',newline='') as w:

我已经更正了代码。 感谢迈克 67 的解决方案。

import csv


def letterGrade(score):
    if score >= 90:
        letter = 'A'
    elif score >= 80:
        letter = 'B'
    elif score >= 70:
       letter = 'C'
    elif score >= 60:
        letter = 'D'
    else:
        letter = 'F'  #fall through or default case
    return letter


    
with open('marks.csv','r') as r:
    csvReader=csv.reader(r)
 
    header=next(csvReader)
    header.append("total")
    
    with open('marks_op.csv','w',newline='') as w:
        csvWriter=csv.writer(w,delimiter=',')
        csvWriter.writerow(header)
   
    total=0
    if header!=None:
        for row in csvReader:
            total=0
            for index in range(1,3):
                thisGrade = row[index]
                if thisGrade == '':
                    thisGrade = 0.0  # change a nothing to a zero
                else:
                    thisGrade = float(thisGrade) 
                total = total + thisGrade
                
            percent = (total  * 100.)/ 200.  # out of a possible 200 points

            gradeToReport = letterGrade(percent)
            
        
            row.append(total)
            
            with open('marks_op.csv','a',newline='') as x:
                csvWriter=csv.writer(x,delimiter=',')
                csvWriter.writerow(row) 


        
        
        
        
            
                
            

 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM