-
-
Save MichelleDalalJian/15dad18def33b4124659aa89a5af5c98 to your computer and use it in GitHub Desktop.
# Use the file name mbox-short.txt as the file name | |
fname = input("Enter file name: ") | |
fhand = open(fname) | |
count = 0 | |
for line in fhand: | |
if line.startswith("X-DSPAM-Confidence:") : | |
count = count + 1 | |
total = 0 | |
for line in fhand: | |
if line.startswith("X-DSPAM-Confidence:"): | |
line = float(line[21:]) | |
total = line + total | |
average = total/ count | |
print("Average spam confidence:",average) |
Hello, First of all, sorry for my English.
effectively seewithacamera should be done only in a "for" bluce, because when using two "for" loops, the second one does not find the content of the open file. It is as if when reading the last line it stopped there, I don't know if I explain myself.
the exercise works for me in the following way:
total = 0.0
count = 0fname = input("Enter file name: ")
fhand = open(fname)for line in fhand:
if line.startswith("X-DSPAM-Confidence:"):
line = float(line[21:])
total = line + total
count = count + 1
#print(total)
average = total / countprint("Average spam confidence:",average)
regards
so u need to open file again
filename=input("whats ur file name")
file=open(filename)
count=0
for line in file:
if line.startswith('X-DSPAM-Confidence:'):
count=count+1
print(count)
tot=0
file=open(filename)
for linex in file:
if linex.startswith('X-DSPAM-Confidence:'):
xx=float(linex[21:])
#print("########")
print(xx)
tot=tot+xx
print(tot)
print("Average spam confidence:",tot/count)
line = float(line[21:])
didnt understand this line of code
line = float(line[21:])
selects the numbers at the end of each line which starts with "X-DSPAM-Confidence:" and converts them to float.
total = line + total
sums all the numbers converted to float one by one using for loopwhat these lines do
hos did you know it was in line 21?
fname = input("Enter file name: ")
fh = open(fname)
total=float(0)
no_of_lines=0
for line in fh:
if line.startswith("X-DSPAM-Confidence:") :
no_of_lines+=1
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
locate=line.find(':') # initially finding the ':' in the line to get the number
total=total+float(line[locate+1:]) # finding sum of the values
print("Average spam confidence:",total/no_of_lines)
fname = input("Enter file name: ")
fh = open(fname)
c=0
s=0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") :
continue
c=c+1
startpos=line.find(":")
digits=line[startpos+1:]
fdigits=float(digits)
s=s+fdigits
av=s/c
print("Average spam confidence:",av)
line = float(line[21:])
selects the numbers at the end of each line which starts with "X-DSPAM-Confidence:" and converts them to float.
total = line + total
sums all the numbers converted to float one by one using for loopwhat these lines do
i didnt get how adding total to line gets the correct output. total is already defined to be 0. so adding 0 to line must be equal to line.
could you please explain the concept of float(line[21:]) and how did that total thing work?
could you please explain the concept of float(line[21:]) and how did that total thing work?
it is basically string slicing. it means it will take the data in the string from the 21st character to the end of the string since we have not given any end. if we had specified like [21:30] so it will take the 21st character to 29th character.
fname = input("Enter file name: ")
try:
fh = open(fname)
except:
print("FILE NOT FOUND..!")
quit()
total = 0
count = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
count = count + 1
line = float(line[20:26])
total = line + total
average = total / count
print("Average spam confidence:", average)
What is total = line + total do
Try this code easy and simpe
fname = input("Enter file name: ")
fhand = open(fname)
total = 0
count = 0
for line in fhand:
if line.startswith("X-DSPAM-Confidence:") :
line = float(line[21:])
total = line + total
count = count + 1
average = total/ count
print("Average spam confidence:", average)
Note: use the file name mbox-short.txt as the file name
SIMPLE AND SHORT
fname = input("Enter file name: ")
fh = open(fname)
total = 0
count = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:") :
line = float(line[21:])
total = line + total
count = count + 1
average = (total / count)
print("Average spam confidence: " + str(average) )
# Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
fh = open(fname)
total = 0
count = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
parts = line.split()
total = float(parts[1]) + total
count = count + 1
print("Average spam confidence:", total/count)
Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
fh = open(fname)
tot = 0
count = 0
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
count+=1
line = float(line[21:])
tot=tot+line
average = tot/count
print("Average spam confidence:",average)
Use mnemonic variable names and a function.
# Use the file name mbox-short.txt as the file name
def getValue(sLine):
c = sLine.find(':')
return float(sLine[c+1:])
fname = input("Enter file name: ")
fh = open(fname)
fSum = 0
nCount = 0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
fValue = getValue(line)
fSum = fSum + fValue
nCount = nCount + 1
# print ("Each:", fValue, fSum, nCount)
print("Average spam confidence:", fSum/nCount)
fname = input("Enter file name: ")
fh = open(fname)
counter =0
result =0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") : continue
index = line.find(":")
value = line[index+1:len(line)]
number = value.strip()
float_number =float(number)
result = result + float_number
counter = counter + 1
print("Average spam confidence: {}".format(result/counter))
Easiest answer:
fil = open('C:\Users\admin\OneDrive\Desktop\mbox-short.txt','r')
cnt = 0
a=0
for ch in fil:
if not ch.startswith('X-DSPAM-Confidence:'):
continue
print(ch.rstrip())
cnt+=1
i=ch[20:]
a+=float(i)
print("Average spam Confidence:",a/cnt)
Use the file name mbox-short.txt as the file name
fname = input("Enter file name: ")
fhand = open(fname)
count = 0
for line in fhand:
if line.startswith("X-DSPAM-Confidence:") :
count = count + 1
total = 0
for line in fhand:
if line.startswith("X-DSPAM-Confidence:"):
line = float(line[21:])
total = line + total
average = total/ count
print("Average spam confidence:",average)
In the above code the logic in the line i couldn't got can anyone explain me please?
line = float(line[21:])
total = line + total
how 21 ?
Is it possible to write this code within single for loop itself?