-
-
Save MichelleDalalJian/4d630b054e647b2d61a5ed9bcc385f10 to your computer and use it in GitHub Desktop.
| 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) |
I'm not even sure how to open this file. I tried converting the data into a txt file and opening it through terminal but it just keeps saying it can't find it.
Actual data: http://py4e-data.dr-chuck.net/regex_sum_2301852.txt (There are 87 values and the sum ends with 524) I need help with this
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))
Actual data: http://py4e-data.dr-chuck.net/regex_sum_2371597.txt (There are 82 values and the sum ends with 487
sum end with 487
i need help with this.
Actual data: http://py4e-data.dr-chuck.net/regex_sum_2371597.txt (There are 82 values and the sum ends with 487 sum end with 487 i need help with this.
You can copy paste my program and replace "Your_file_name" by your URL, it should work.
Ps: don't forget to indent correctly ;)
My prompt was
"Actual data: http://py4e-data.dr-chuck.net/regex_sum_2076089.txt (There are 79 values and the sum ends with 942)"
`import re
import urllib.request
url = 'https://py4e-data.dr-chuck.net/regex_sum_2076089.txt'
response = urllib.request.urlopen(url)
data = response.read().decode()
numbers = re.findall('[0-9]+', data)
total_sum = sum(int(num) for num in numbers)
print("Sum:", total_sum)
`
Sum = 391942
This one works