Task: To find all the numbers in a text file and compute the sum of it.
Link to file(if required) : http://python-data.dr-chuck.net/regex_sum_42.txt
name = raw_input("Enter your file: ")
if len(name) < 1: name = "sample.txt"
try:
open(name)
except:
print "Please enter a valid file name."
exit()
import re
lst = list()
for line in name:
line = line.strip() #strip() instead of rstrip() as there were space before line as well
stuff = re.findall("[0-9]+", line)
print stuff # i tried to trace back and realize it prints empty list so problem should be here
stuff = int(stuff[0]) # i think this is wrong as well
lst.append(stuff)
sum(lst)
print sum(lst)
Can someone tell me where did I go wrong ? sorry for any formatting errors and thanks for the help
I have also tried:
\s[0-9]+\s
.[0-9]+.