Created
April 7, 2023 19:37
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`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))`