Skip to content

Instantly share code, notes, and snippets.

@deleonab
Created March 21, 2022 07:31
Show Gist options
  • Save deleonab/326d41b8c62c7b18d1e8233752589d4d to your computer and use it in GitHub Desktop.
Save deleonab/326d41b8c62c7b18d1e8233752589d4d to your computer and use it in GitHub Desktop.
11.2 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 testing and the other is the actual data …
import re
my_file = open('regex_sum_1511276.txt')
myreadfile = my_file.read()
myextractednums = re.findall('[0-9]+',myreadfile)
totalnums = 0
for mynum in myextractednums:
totalnums = totalnums + int(mynum)
print(totalnums)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment