count = 0
fname = input("Enter file name: ")
fh = open(fname)
for line in fh:
if line.startswith("X-DSPAM-Confidence:") :
print(line)
value = line[line.rfind(':'):] # will take the last occurrence of : to slice the line
print(value)
count = count + 1
print(count)
count = 0
fname = input("Enter file name: ")
fh = open(fname)
for line in fh:
if line.startswith("X-DSPAM-Confidence:") :
print(line)
value = line.split(':')[-1] # will split line into 'X-DSPAM-Confidence' and 'value'
# if you have ',' at the end of the line, simply do this:
value = value.strip(',')
value = float(value)
print(value)
count = count + 1
print(count)
只要格式与您描述的完全一致,就可以使用以下代码:
如果文本中的变化比您描述的更多,您可能需要尝试regex。你知道吗
您可以使用
str.rfind(':')
来获取:
的位置,然后执行字符串切片来获取值。你知道吗尝试使用
split(':')
:代码:
相关问题 更多 >
编程相关推荐