Python:ValueError:对关闭的文件进行I / O操作。 (文本文件)

时间:2019-02-06 18:27:04

标签: python-3.x csv text error-handling sum

import csv
csv_file = 'Annual Budget.csv'
txt_file = 'annual_budget.txt'
with open(txt_file, 'w') as my_output_file:
with open(csv_file, 'r') as my_input_file:
    reader = csv.reader(my_input_file)
    for row in reader:
        my_output_file.write(" ".join(row)+'\n')
        data = []
with open(r'annual_budget.txt', 'r') as f:
 reader = csv.reader(f)
 header = next(reader) 

 def isfloat(value):
         try:
             float(value)
             return True
         except ValueError:
             return False 
for line in reader:
rowdata = map(float,line)
data.extend(rowdata)
print(sum(data)/len(data))

试图使程序在文本文件中添加数字。我希望它执行其他数学运算,但我正在逐步进行。输出应为所有数字的总和。 [![https://i.stack.imgur.com/FY7WI.png][1]][1]

0 个答案:

没有答案