Created
October 7, 2017 14:48
-
-
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…
This file contains hidden or 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 | |
| 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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))