Skip to content

Instantly share code, notes, and snippets.

@zcakzwa
Created July 5, 2016 03:59
Show Gist options
  • Select an option

  • Save zcakzwa/19a448416b9e7da3dc2096da72690914 to your computer and use it in GitHub Desktop.

Select an option

Save zcakzwa/19a448416b9e7da3dc2096da72690914 to your computer and use it in GitHub Desktop.
7.2 Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: X-DSPAM-Confidence: 0.8475 Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or …
# Use the file name mbox-short.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
t=line.find("0")
number= float(line[t:])
count = count + 1
total = total + number
average = total/count
print "Average spam confidence:",average
@Aida-p
Copy link

Aida-p commented Dec 13, 2022

I solved abit diffrent it worked in python 3.10.8 but not on the class online tools :

`fname= input("Enter the file name: ")
if len(fname) < 1:
fname = ("mbox-short.txt")
fh = open (fname)
new_line = []
for line in fh :
if not line.startswith("X-DSPAM-Confidence:"):
continue
# print(line)
line_result = line.rstrip().removeprefix("X-DSPAM-Confidence:")
new_line.append(line_result)

    Sum_all_line  = 0
    for i in range(len(new_line)):
        new_line[i] = float(new_line[i])
        Sum_all_line = Sum_all_line +new_line[i]

print(Sum_all_line)
total_avarage = Sum_all_line / len( new_line)

print(new_line)

print(total_avarage)
print("Done")`

@zykaj
Copy link

zykaj commented Jan 7, 2023

In python 3 use the below code

count = 0
total = 0

name = input("Enter file name: ")
txt_file = open(name)
for line in txt_file:
if line.startswith("X-DSPAM-Confidence:"):
a = line.find(" ") + 1 # Find the first space character
b = line.find(" ", a) # Find the second space character
c = float(line[a:b]) # Extract the float value between the two spaces
total += c
count += 1
ave = total/count
print("Average spam confidence:", ave)

@sagdi
Copy link

sagdi commented Feb 12, 2023

you can do it like this

#Use the file name mbox-short.txt as the file name

fname = input("Enter file name: ")
fh = open(fname)
total = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):
continue
x = line.find(":")
y = float(line[x+1 : ])
total = total + y
res = total / len(line)
print("Average spam confidence:", res)

@Adel-1984
Copy link

hello everyone
please help me with this error
Screenshot 2023-03-02 085929

@ShuckZ77
Copy link

Use the file name mbox-short.txt as the file name

fname = input("Enter file name: ")

fh = open(fname)

count=0
shum=0

for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):
continue

#print(line)
#print(line.find('0'))

count = count+1

 
linewa = (line[20:])

shum = shum+float(linewa)

#print(count,shum)

print('Average spam confidence:',shum/count)

@tahira2k16
Copy link

fn = input("Enter file name: ")
fh = open(fn)

lines = 0
counts = 0

for x in fh:
if x.startswith("X-DSPAM-Confidence:"):
counts += float(x.replace("X-DSPAM-Confidence:", "").lstrip())
lines+=1

print("Average spam confidence:" , counts/lines)

@asmautino
Copy link

fname = input("Enter file name: ")
fh = open(fname)
linecount=0
num=0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):
continue
sub=":"
indice=line.index(sub)
conf=float(line[indice+2:])
linecount=linecount+1
num=num+conf
avg=num/linecount
print("Average spam confidence:",avg)

@Adel-1984
Copy link

Adel-1984 commented Jun 22, 2023 via email

@Deepanshu1920
Copy link

fname = input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
number_float = 0
for line in fh:
xli = line.find("X-DSPAM-Confidence:")

if xli == 0:
    ary = line.split(': ')

    number_float = float(ary[1]);
    count = count + 1
    total = total + number_float

avg = total / count
print ("Average spam confidence:", avg)

@retf1
Copy link

retf1 commented Jul 19, 2023

hello everyone please help me with this error Screenshot 2023-03-02 085929

you missed continue (if line)

@theoriginalkamdia
Copy link

fname = raw_input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
t=line.find("0")
number= float(line[t:])
count = count + 1
total = total + number

average = total/count

print ("Average spam confidence:",average)fname = raw_input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
t=line.find("0")
number= float(line[t:])
count = count + 1
total = total + number

average = total/count

print ("Average spam confidence:",average)

@Inspire2023
Copy link

Use the file name mbox-short.txt as the file name

file_name = input("Enter file name: ")

try:
file_handle = open(file_name,"r")
except:
print("File Does not Exist!")

count = 0
total_num = 0

for line in file_handle:

if not line.startswith("X-DSPAM-Confidence:"):
    continue
#print(line)

count +=1
#print(count)

space_position = line.find(" ")
#print(space_position)

num = float(line[space_position:])
#print(num)

total_num = total_num + num
#print(total_num)

print("Average spam confidence:", total_num/count)

@sujatha666
Copy link

Use the file name mbox-short.txt as the file name

fname = input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):continue
c=float((line[line.find("0"):]))

count = count + 1
total = total + c

print("Average spam confidence:", total/count)
Screenshot 2024-11-25 155748

@crispy-py
Copy link

guys use this:
fname = input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
t = line.find('0')
number = float(line[t:])
count = count + 1
total = total + number
else:
continue
average = total/count
print("Average spam confidence:", average)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment