-
-
Save zcakzwa/19a448416b9e7da3dc2096da72690914 to your computer and use it in GitHub Desktop.
# 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 |
# Use the file name mbox-short.txt as the file name fname = input("Enter file name: ") fh = open(fname) count = 0 average = 0 for line in fh: if not line.startswith("X-DSPAM-Confidence:") : continue average += float(line[20:-1].strip()) count = count + 1 #print(line) print("Average spam confidence:", (average/count))hey thanks it works.
Does anyone know what the " +=" does in this case??? I'm really confused
'a+=b' means 'a=a+b'
fname = input("Enter file name: ") fh = open(fname) lst=[] for line in fh: if not line.startswith("X-DSPAM-Confidence:") : continue b=line #print(b) x=b.find('0') host=b[x:] #print(host) flt=float(host) #print(flt) lst.append(flt) #print(lst) total=0 count=0 for i in lst: total=i+total count=count+1 #print(count) print("Average spam confidence:",total/count)
Detailed steps for learners
does anyone mind explaining this line ---> x=b.find('0')? especially why it's 0 in the parenthsis?
Thank you!
Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
try:
fopen = open(fname)
except:
print("Searched file doesnt exist!")
quit()
countlines = 0
totalvalues = 0
for line in fopen:
if not line.startswith("X-DSPAM-Confidence:"):
continue
countlines = countlines + 1
keyletter = line.find(":")
linevalue = float(line[keyletter+1:])
totalvalues = totalvalues + linevalue
avg = totalvalues / countlines
print("Average spam confidence:",avg)
fname = input('Enter file name:')
fh = open(fname,'r')
count=0
avg=0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):
continue
else:
count=count+1
line=line.rstrip()
cpos=line.find(':')
line=line[cpos+2:]
avg=avg+float(line)
print("Average spam confidence:",avg/float(count))
can someone tell me how to do this?? i just can't figure out how to extract the numbers only from the string.
Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
fh = open(fname)
count = 0
for line in fh:
line = line.rstrip()
line = line.find(float())
if not line.startswith("X-DSPAM-Confidence:"):
continue
count = count +1
print(line, count)
print("Average spam confidence: ")
fname = input("Enter file name: ")
fh = open(fname)
number_of_lines = 0
values = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
values += float(line.replace("X-DSPAM-Confidence:", "").lstrip())
number_of_lines = number_of_lines +1
print("Average spam confidence:" , values/number_of_lines)
fname = input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
for line in fh:
#print(line)
if not line.startswith("X-DSPAM-Confidence:"):
continue
pos = line.find('0')
number = float(line[pos:])
total = total + number
count = count + 1
avg = total/count
print("Average spam confidence:",avg)
Hi, this is my code:
Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
fh = open(fname)
num = 0
prom = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
x = line.find('0')
y = line [x:]
f = float(y)
num = num + 1
prom = float(prom) + f
average = prom/num
print("Average spam confidence:",average)
the output is = Average spam confidence: 0.7507185185185187
This worked for me:
fname = input("Enter file name: ")
fh = open(fname)
count = 0
total = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):
continue
else:
a = line.find(':')
line = line [a+1 :]
b = line.strip()
c = float(b)
count = count + 1
total = total + c
print("Average spam confidence:", total/count)
The output will be: Average spam confidence: 0.7507185185185187
Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
fh = open(fname)
count = 0
num = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
count += 1
line = line.rstrip()
index = line.find("0")
string = line[index:]
num += float(string)
print("Average spam confidence:", num / count)
I am literally lost when it comes to this. Someone please guide me, tell me what I'm doing wrong in terms of which established coding rule I'm violating or failing to adhere to. I've seen identical codes achieving the desired output yet mine is totally wrong.
Just try to to delete print(line). The output should be only the average!
This is my code, and it can work.
try: # get a safe program
fhand = open(fname)
except:
print("File cannot be opened:", fname)
exit()
count = 0
total = 0
for line in fhand:
if line.startswith("X-DSPAM-Confidence:"): # grab the line with float number and remove the whitspace
line = line.strip()
if not line.startswith("X-DSPAM-Confidence:"):# exclude the line without float number
continue
p_0 = line.find("0") # find the position of zero
num = float(line[p_0:]) # grab the float number using slice and change it to the float type
total = total + num # get total number by adding all the float number
count = count+1
average = total/count
print("Average spam confidence:", average) ```
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
t = line.find("0")
number = float(line[t:])
count = count + 1
total = total + number
average = total/count
print("Average spam confidence:" , average )
count = 0
total = 0
name = input("Enter file name: ")
txt_file = open(name)
for line in txt_file:
print(line) -- if we print until this we will have full file
if line.startswith("X-DSPAM-Confidence:"):
print(line) -- then we only need line start with "X-DSPAM-Confidence:" , in this why we use if condition
a = line.find("0")
print (a) -- now we fine the position of 0 in line (remember we only need number to count and for Ave)
b = len(line)
print(b) -- this with help me to fine the length of the line
c = float(line[a : b])
print(c) -- Now we have number to count and ave
total = total + c
count = count + 1
until we have total and total number of line (count)
ave = total/count
print("Average spam confidence:", ave)
if we use want to learn go line by line and try to under stand
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")`
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)
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)
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)
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)
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)
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)
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)
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)
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
fname = input("Enter file name: ")
try:
fh = open(fname)
except:
print('File cannot be opened:', fname)
quit()
count = 0
total = 0
for line in fh:
if line.startswith('X-DSPAM-Confidence:'):
count = count + 1
num = float(line[line.find(' '):].lstrip())
total = total + num
print('Average spam confidence:',
[ ]
@@`(total/count))```