138k views
3 votes
Write a function called csv_sum that takes a filename and returns the sum of all of the numbers in the file. The numbers are in csv format. For instance, if the contents of the file are: 12,3,2 -5 10,20,-10,8.3 Then the function should return 40.3.

User Egos Zhang
by
6.9k points

1 Answer

2 votes

Answer:

Step-by-step explanation:

def csv_sum(filename):

total = 0

try:

f = open(filename)

for line in f:

words = line.strip().split(",")

for word in words:

total += float(word)

f.close()

except FileNotFoundError:

pass

return total

User Semo
by
6.5k points