【发布时间】:2020-07-13 03:12:23
【问题描述】:
字符串 = 概率为 0.05 如何在变量中提取 0.05 浮点值?文件中有很多这样的字符串,我需要求平均概率,所以 我使用了“for”循环。 我的代码:
fname = input("enter file name: ")
fh = open(fname)
count = 0
val = 0
for lx in fh:
if lx.startswith("probability"):
count = count + 1
val = val + #here i need to get the only "float" value which is in string
print(val)
【问题讨论】:
-
您遗漏了重要部分:您的字符串外观的完整示例。通过猜测它会说它类似于
'probability 0.71'? -
float(lx.strip().split()[-1])
标签: python python-3.x string file extract