Skip to content

Instantly share code, notes, and snippets.

@donlovett
Created September 16, 2016 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donlovett/948b277050ecffe3aa1b2f7804a84b4a to your computer and use it in GitHub Desktop.
Save donlovett/948b277050ecffe3aa1b2f7804a84b4a to your computer and use it in GitHub Desktop.
Update with some comments and debug code disabled
# Search for lines that have numbers with a regular expression, add to an array, convert to integers and count and sum
# Author Don lovett Based on Code from Using Python to Access Web Data text by Charles Severance
import re
#fname = raw_input('Enter file:') Use for prompted file name
#hand = open(fname)
hand = open("/home/vagrant/Coursera/Week2/regex_sum_318168.txt")
nums = list()
z = 0
for line in hand:
line = line.rstrip()
x = re.findall('([0-9]+)', line)
y = len(x)
#if len(x) > 0 :
if y > 0 :
for z in range (0 , y):
val = int(x[z])
nums.append(val)
#print (nums)
print len(nums)
print sum(nums)
#for item in nums[:len(nums)]:
# print item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment