0

I have this error "invalid literal for int() with base 10: '1.0'" and I don't know how to fix it. I need to do summation of all values for each 'key' in 'mhist_goodbags'.

# Import Packages
import csv


mhist_goodbags ={}

with open('Test.csv','r') as f:
    next(f)
    filereader = csv.reader(f)
   
    for line in filereader:
        GoodBags = line[1]
        BadBags = line[2]
        Product_Case = line [5]
        Errors = line[6]
        day_time = line[0]
        if 'Product' in Product_Case and 'NoError' in Errors:
            Hour = day_time[10:13]
            k = Hour
            if k in mhist_goodbags.keys():
                mhist_goodbags[k] += int (GoodBags)
            else:
                mhist_goodbags[k] = int (GoodBags)

If I replace 'mhist_goodbags[k] += int (GoodBags)' and 'mhist_goodbags[k] = int (GoodBags)' to 'mhist_goodbags[k] += GoodBags' and 'mhist_goodbags[k] = GoodBags', I will get this output:

{' 00': '1.01.01.00.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.01.00.01.01.01.01.01.01.01.01.01.01.01.01.01.00.01.01.01.01.01.00.00.01.00.00.00.01.00.00.00.00.01.0, ' 01': '1.00.00.00.00.00.01.01.00.01.00.01.01.00.00.00.01.00.00.01.00.01.00.01.01.01.00.00.01.01.01.00.01.01.00.01.01.01.00.01.01.01.01.01.00.01.01.00.00.00.01.01.00.00.00.01.01.00.01.00.01.01.01.01.00.01.01.00.00.01.00.01.00.01.01.00.01.00.00.00.01.00.00.00.01.00.00.01.00.01.00.00.01.01.01.00.00.01.00.01.01.00.00.00.00.00.0, ' 02': '1.01.01.01.00.00.01.00.00.01.00.00.00.01.01.01.01.00.01.01.00.01.01.00.01.00.01.00.00.01.01.00.01.01.00.00.00.00.00.01.01.00.01.00.00.01.01.00.00.00.01.00.01.01.01.01.00.01.01.01.01.00.01.00.00.01.00.00.01.00.00.00.01.01.00.01.00.00.00.00.01.01.01.01.00.00.00.01.01.01.00.00.00.01.00.00.01.00.00.00.00.01.01.00.01.00.00.01.01.01.00.00.01.01.01.00.00.01.01.01.00.01.00.00.00.00.00.01.01.01.00.00.00.01.00.00.01.00.01.01.00.00.00.00.00.01.01.00.01.01.01.01.00.01.01.00.01.01.01.00.00.01.00.01.01.01.01.01.01.00.01.01.00.00.01.01.00.01.01.01.01.00.00.00.00.00.01.00.0}

I am looking to do summation of all values of each key in a 'mhist_goodbags' dictionary.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mohamed
  • 51
  • 5

0 Answers0