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]