TheHoff
TheHoff

Reputation: 61

how to get the sum of a CSV column list to print

I need to get the sum of the second column in a csv. I have tried several possible solution but at most, I am only getting Totals = set{} where I would expect to get the sum of the list I created from the second column of the csv file. My csv file looks like this: enter image description here

I am sure it has to be a simple solution I am missing, but I can't figure it out and have spent hours trying different options I've found, here and on other sites. I would really appreciate any help to point me in the right direction. Also, for this, I can't use modules like Pandas. Here is my code.

    import os
    import csv

    budget_csv = os.path.join("../PyBank", "budget_data.csv")

    with open(budget_csv, newline="") as csvfile:
        csvreader = csv.reader(csvfile, delimiter=",")
        #skip header row
        next(csvreader, None) 

        # Gets number of rows in csv
        data=list(csvreader)

        row_count = len(data)

        totals = []
        for row in csvreader:
            values = row[1]
            totals.append(values)

        print (totals) #did this just to see if it would print the list of values



    print ("Financial Analysis")
    print ("-------------------------------")
    print ("Total Months: ", row_count)
    print ("Total: ", sum(totals))
    print ("Average Change: ")
    print ("Greatest Increase in Profits: ")
    print ("Greatest Decrease in Profits: ")



    output_file = os.path.join("Analysis.txt")

    #  Open the output file
    with open(output_file, "w") as text_file:
        text_file.write (f"Financial Analysis\n")
        text_file.write (f"-------------------------------\n")
        text_file.write (f"Total Months: " + str(row_count) + "\n")
        #print (f"Total: {}".format(net))
        #print (f"Average Change: ")
        #print (f"Greatest Increase in Profits: ")
        #print (f"Greatest Decrease in Profits: ")  

Upvotes: 1

Views: 1222

Answers (3)

TheHoff
TheHoff

Reputation: 61

Figured it out, finally. Created two new variables. Renamed totals = [] to totals1 = []. Then turned the ints in totals 1 into floats using totals2 = [float(integral) for integral in totals1], which was used to get the sum with totals3 = sum(totals2). New code:

     totals1 = [] for row in data:
         values = row[1] 
         totals1.append(values)          
     totals2 = [float(integral) for integral in totals1] #turns values in totals1 into floats
     totals3 = sum(totals2) 

Upvotes: 1

buran
buran

Reputation: 14273

when you do data=list(csvreader) you consumed csvreader, so it's at the end of the file and when you try to iterate over it again on this line for row in csvreader: it's empty and total remains empty list. I would also recommend using csv.DictReader.

Not tested, bur something like

import os
import csv

budget_csv = os.path.join("../PyBank", "budget_data.csv")

with open(budget_csv, 'r') as csvfile:
    csvreader = csv.DictReader(csvfile, delimiter=",")
    totals = []
    for row_count, row in enumerate(csvreader, start=1):
        value = int(row['Profit/Losses'])
        totals.append(value)


print ("Financial Analysis")
print ("-------------------------------")
print ("Total Months: {}".format(row_count))
print ("Total: {}".format(sum(totals)))
print ("Average Change: ")
print ("Greatest Increase in Profits: ")
print ("Greatest Decrease in Profits: ")



output_file = os.path.join("Analysis.txt")

#  Open the output file
with open(output_file, "w") as text_file:
    text_file.write ("Financial Analysis\n")
    text_file.write ("-------------------------------\n")
    text_file.write ("Total Months: {}\n".format(row_count))

Upvotes: 0

user2652620
user2652620

Reputation: 464

you need to iterate on data, not on csvreader once csvreader is cast as list.

for row in data:

Upvotes: 1

Related Questions