見つけるのではなく、すべての 'X-DSPAM-Confidence' 数値を平均するよう求めているようです0.750718518519
。
個人的には、探している単語を見つけて数字を抽出し、これらすべての数字をリストに入れて、最後に平均化します。
このようなもの -
# Get the filename from the user
filename = raw_input("Enter file name: ")
# An empty list to contain all our floats
spamflts = []
# Open the file to read ('r'), and loop through each line
for line in open(filename, 'r'):
# If the line starts with the text we want (with all whitespace stripped)
if line.strip().startswith('X-DSPAM-Confidence'):
# Then extract the number from the second half of the line
# "text:number".split(':') will give you ['text', 'number']
# So you use [1] to get the second half
# Then we use .strip() to remove whitespace, and convert to a float
flt = float(line.split(':')[1].strip())
print flt
# We then add the number to our list
spamflts.append(flt)
print spamflts
# At the end of the loop, we work out the average - the sum divided by the length
average = sum(spamflts)/len(spamflts)
print average
>>> lines = """X-DSPAM-Confidence: 1
X-DSPAM-Confidence: 5
Nothing on this line
X-DSPAM-Confidence: 4"""
>>> for line in lines.splitlines():
print line
X-DSPAM-Confidence: 1
X-DSPAM-Confidence: 5
Nothing on this line
X-DSPAM-Confidence: 4
検索の使用:
>>> for line in lines.splitlines():
pos = line.find('X-DSPAM-Confidence:')
print pos
0
0
-1
0
各行find()
の位置を示しているだけで、その後の数値の位置を示していないことがわかります。'X-DSPAM-Confidence:'
行が で始まる場合は見つけやすく'X-DSPAM-Confidence:'
、次のように番号だけを抽出します。
>>> for line in lines.splitlines():
print line.startswith('X-DSPAM-Confidence')
True
True
False
True
>>> for line in lines.splitlines():
if line.startswith('X-DSPAM-Confidence'):
print line.split(':')
['X-DSPAM-Confidence', ' 1']
['X-DSPAM-Confidence', ' 5']
['X-DSPAM-Confidence', ' 4']
>>> for line in lines.splitlines():
if line.startswith('X-DSPAM-Confidence'):
print float(line.split(':')[1])
1.0
5.0
4.0