简体   繁体   English

无法将数据写入 csv 文件

[英]Not able to write data into csv file

I am not able to write data into csv file.我无法将数据写入 csv 文件。 I was able to separate the header and add new column and also was able to add the total scores, but seems like it is not working我能够分离 header 并添加新列,还能够添加总分,但似乎它不起作用

Source File源文件

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

Target File目标文件

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)
            

You're overwriting the output file in each loop iteration您在每次循环迭代中覆盖 output 文件

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

should be应该

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

I have corrected the code.我已经更正了代码。 Thanks for solution Mike 67.感谢迈克 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