Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AreebaYousuf/709f45072514cc2ae13eb7be0d45e5f0 to your computer and use it in GitHub Desktop.
Save AreebaYousuf/709f45072514cc2ae13eb7be0d45e5f0 to your computer and use it in GitHub Desktop.
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 you n…
import re
fh=open('week_1.txt')
l=list()
for digits in fh:
digits=digits.rstrip()
stuff=re.findall('[0-9]+',digits)
if len(stuff)>0:
l=l+stuff
sum=0
for n in l:
sum=sum+int(n)
print(sum)
@AreebaYousuf
Copy link
Author

`import re

hand = open("regex_sum_1778498.txt")
x=list()
for line in hand:
y = re.findall('[0-9]+',line)
if len(y)>1:
x=x+y
print(x)
out=list()
for value in x:
out.append(float(value))
print(out)
print(sum(out))`

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