Created
September 16, 2016 21:05
-
-
Save donlovett/948b277050ecffe3aa1b2f7804a84b4a to your computer and use it in GitHub Desktop.
Update with some comments and debug code disabled
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
# 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