Adding numbers from a file - Python
Question: I am trying to add numbers by reading a file.

The file has texts and numbers, so I need only numbers and add them to display
the total.

It is always showing the value as zero. Don't know where I do wrong.

Here's the code:

    
    
    import re
    file1 = open('Actual.txt')
    line = file.read().rstrip()
    numlist = list()
    for line in file1:
        list = re.findall(r'[0-9.]+',line)
        if len(list)>0:
            num = int(list)
            numlist.append(num)
    total = 0
    for sum in numlist:
        total += sum
    print (total)
    


Answer: This may help you:

    
    
    import re
    file1 = open('so.txt')
    
    numlist = list()
    for line in file1:
        line_list = re.findall(r'[0-9.]+',line)
        if len(line_list) > 0:
            numlist.extend(line_list)
    
    print numlist
    total = 0
    for sum in numlist:
        total += int(sum)
    print (total)
    

