Search code examples
pythonfloating-pointsumstring-conversion

Python re.findall float return unable to sum returned values


I'm using re.findall to return the float from a specific line in a file:

mealReturnValue = re.findall("\d+\.\d+",lines[2])

Example file line it pulls from: "The total bill was 400.00 "

This will return a list of numbers as follows:

[['400.00'], ['210.0'], ['400.00']]

I'm then trying to sum the list, however, it throws the following error:

Traceback (most recent call last):
  File "TotalSpend.py", line 25, in <module>
    totalSpend = sum(mealReturn)
TypeError: unsupported operand type(s) for +: 'int' and 'list'

This seems to be a function of the re.findall as i've been able to do this manually by generating an array of the same numbers.

For example:

i = [400.00, 210.0, 400.00]

print sum(i)

So i don't know what the real problem is here, i cant seem to convert the re.findall to a float i can use in the list.

Any help would be greatly appreciated.

For extra clarity the entire code file:

import os
import sys
import re


dirListing = os.listdir('C:\Users\joshnathan\Desktop\LPTHW')
mealReturn =[] # Array of returned meal values
numberOfMeals = 0

print "Which meal do you want to calculate?"
meal = raw_input(">>")


for item in dirListing:
    if meal in item:
        numberOfMeals += 1
        fileName = open(item, 'r')
        lines = fileName.readlines()
        mealReturnValue = re.findall('\d+\.\d+',lines[2])
        mealReturn.append(mealReturnValue)
        print mealReturn


print numberOfMeals
totalSpend = sum(mealReturn)
print totalSpend

Thanks Joshua


Solution

  • Try this.

    In [1]: sum([float(i[0]) for i in a])
    Out[1]: 1010.0
    

    Considered a as your result. You are trying to perform sum operation on list of list. And it's in string format. You need to convert to float and make it as a single list. Then you can perform the sum operation.

    Steps

    1 . Make it as a single list

    In [2]: [i[0] for i in a]
    Out[2]: ['400.00', '210.0', '400.00']
    

    2 . Convert all elements to float

    In [3]: [float(i[0]) for i in a]
    Out[3]: [400.0, 210.0, 400.0]
    

    3 . Perform the sum operation.

    In [4]: sum([float(i[0]) for i in a])
    Out[4]: 1010.0
    

    Edit

    mealReturnValue = re.findall('\d+\.\d+',lines[2]) will return something like this [['400.00']] So you can convert it into float there itself like this mealReturnValue = float(re.findall('\d+\.\d+',lines[2])[0]). And instead of append into a string you can add up on a variable. mealReturnValue += re.findall('\d+\.\d+',lines[2]). To use like this you have to declare the mealReturnValue. Like this mealReturnValue = 0

    Change you code like this

    totalSpend = 0
    for item in dirListing:
        if meal in item:
            numberOfMeals += 1
            fileName = open(item, 'r')
            lines = fileName.readlines()
            totalSpend += float(re.findall('\d+\.\d+',lines[2])[0])
    
    print numberOfMeals
    print totalSpend