Karan Bari
Karan Bari

Reputation: 75

The output always comes out to be an empty list

Exercise 2: Write a program to look for lines of the form:

New Revision: 39772

Extract the number from each of the lines using a regular expression and the findall() method.

Compute the average of the numbers and print out the average.

The input file is "mbox-short.txt".

import re
fname=input("enter the file name:")
fhandle=open(fname)
total=0
count=0
for x in fhandle:
    y=re.findall("^New Revision:(\s [0-9]*) ",x)         
    total=total+y
    count=count+1
print(y)
print("total and average is:",total,total/count)

Also can anyone suggest that how to convert all the elements in the list to float

Upvotes: 1

Views: 112

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51653

Your regex had a ' ' too much at its end, thats why it did never find something.

findall() with the pattern you startet will ever only return 1 hit (or 0 hits) - due to starting with ^.

import re

fname=input("enter the file name:")
fhandle=open(fname)
total=0.0
count=0
for x in fhandle:
    y=re.findall(r"^New Revision:(\s*[0-9]*)",x)    # 0 or 1 hit  
    if y:  
        total += float(y [0] )               # to float if found 
        count += 1                           
print("total and average is:",total,total/count)

Example using a text:

t = """New Revision: 129
New Revision: 129
New Revision: 129
New Revision: 129
New Revision: 129
"""

import re 
total=0.0
count=0
for x in t.splitlines():
    y=re.findall("^New Revision:(\s*[0-9]*)",x)
    if y:
        total += float(y [0] )
        count += 1
print(y)
print("total and average is:",total,total/count)

Output:

total and average is: 645.0 129.0

Upvotes: 1

Related Questions