Extreme Thinking
Rython import re



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.

網路檔案時 ….

import re, requests
r = requests.get('http://py4e-data.dr-chuck.net/regex_sum_694846.txt')
print (sum([ int(x) for x in re.findall('([0-9]+)',r.text)]))

本地檔案時 ….

import re
with open('test.txt','r') as f:
    a = f.read()
print (sum([ int(x) for x in re.findall('([0-9]+)',a)]))

檔案大時要用 f.readlines() 這次網路下載檔案不大還是寫一下

import re
d = 0
with open('test.txt','r') as f:
    for line in f.readlines():
        d = d + sum([ int(x) for x in re.findall('([0-9]+)',line)])
print (d)