-
Notifications
You must be signed in to change notification settings - Fork 0
/
py4e_1102.py
30 lines (28 loc) · 1012 Bytes
/
py4e_1102.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
import re
ffile=input("please enter the path to your file \n>>>")
myfile=open(ffile,'r')
regex="New Revision: (\d+)"
#flist=list()
summed=float()
counter=0
for line in myfile:
line=line.rstrip()
x = re.findall(regex,line)
if len(x)>0:
#flist.append(x)
summed+=float(x[0])
counter+=1
print (summed/counter)
'''
Here's what's going on here:
1- Obviously, we input the path to the file that needs analysis.
2- Next, input the regex that we use on this file. Note that this regex can also be
fed directly into the loop below. But I like the aesthetics of using a variable named
regex in the loop.
3- The for loop goes through the file line by line. In each line the specified regex is
extracted and after excluding matches of len(0), is sent down for further processing.
4- the result we have here is a list with a str(number). So, we extract that item
as float, and += it in summed.
5- finally, since we are taking an average here we need a counter.
'''