1

I'm just learning about regular expressions and I need to read in a text file and find every instance of a number and find the sum of all the numbers.

import re

sum = 0
list_of_numbers = list()
working_file = open("sample.txt", 'r')
for line in working_file:
    line = line.rstrip()
    working_list = re.findall('[0-9]+', line)
    if len(working_list) != 1:
        continue
    print(working_list)
    for number in working_list:
        num = int(number)
        list_of_numbers.append(num)
for number in list_of_numbers:
    sum += number
print(sum)

I put the print(working_list) in order to try and debug it and see if all the numbers are getting found correctly and I've seen, by manually scanning the text file, that some numbers are being skipped while others are not. I'm confused as to why as I thought my regular expression guaranteed that any string with any amount of digits will be added to the list.

Here is the file.

2
  • 1
    if len(working_list) != 1 ... im pretty sure thats not what you want... note you *really dont even have to check if the working_list has numbers at all... Commented Jan 3, 2018 at 22:36
  • so, you just need to do total = sum(int(x) for line in working_file for x in re.findall('[0-9]+', line)) Commented Jan 3, 2018 at 22:38

3 Answers 3

6

You're only validating lines that have ONLY one number, so a line with two numbers will be skipped because of if len(working_list) != 1: continue, that basically says "if there isn't EXACTLY one number on this line then skip", you may have meant something like if len(working_list) < 1: continue

Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow! Silly error on my part I don't know how I didn't see that! Thank you!!
0

I would do it like:

import re

digits_re = re.compile(r'(\d+(\.\d+)?)') 
with open("sample.txt", 'r') as fh:
  numbers = [float(match[0]) for match in digits_re.findall(fh.read())]
print(sum(numbers))

or like you're doing with ints just

import re

digits_re = re.compile(r'(\d+)') 
with open("sample.txt", 'r') as fh:
  numbers = [int(match[0]) for match in digits_re.findall(fh.read())]
print(sum(numbers))

Comments

0
h = open('file.txt')
nos = list()
for ln in h:
    fi = re.findall('[0-9]+', ln)
    for i in fi:
        nos.append(int(i))
print('Sum:', sum(nos))

1 Comment

Welcome to Stack Overflow! Code-only answers are not particularly helpful. Please add some descriptions of how this code solves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.