Skip to content

Instantly share code, notes, and snippets.

@MichelleDalalJian
Created October 7, 2017 14:48
Show Gist options
  • Select an option

  • Save MichelleDalalJian/4d630b054e647b2d61a5ed9bcc385f10 to your computer and use it in GitHub Desktop.

Select an option

Save MichelleDalalJian/4d630b054e647b2d61a5ed9bcc385f10 to your computer and use it in GitHub Desktop.
Extracting Data With Regular Expressions Finding Numbers in a Haystack In this assignment you will read through and parse a file with text and numbers. You will extract all the numbers in the file and compute the sum of the numbers. Data Files We provide two files for this assignment. One is a sample file where we give you the sum for your testi…
import re
hand = open("regex_sum_24962.txt")
x=list()
for line in hand:
y = re.findall('[0-9]+',line)
x = x+y
sum=0
for z in x:
sum = sum + int(z)
print(sum)
@Trigis
Copy link

Trigis commented Oct 24, 2025 via email

@LeDonBobo
Copy link

Hello,
As there are many answers to this assignment, mine is the one from a noob that coudln't write a code 1 month ago so it's not that sofisticated but it works !
import re
hand = open("Your_file_name")
lst = list()
for line in hand:
stuff = re.findall("[0-9]+",line)
for num in stuff:
lst.append(int(num))
print("Sum = ",sum(lst))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment