0

节目详情:

我正在为 python 编写一个程序,该程序需要查看该行的文本文件:

找到模式 1 of 12:EV= 1.5185449E+04,f= 19.612545,T= 0.050988。

问题:

然后在程序找到该行之后,它将将该行存储到一个数组中并从 f = 19.612545 中获取值 19.612545。

问题:

到目前为止,我已经能够在找到它之后将它存储到一个数组中。但是,在存储字符串以搜索字符串,然后从变量 f 中提取信息后,我无法确定使用什么。有没有人对如何实现这一点有任何建议或提示?

4

2 回答 2

5

根据您的想法,CosmicComputer 将您推荐给正则表达式是正确的。如果您的语法如此简单,您总是可以执行以下操作:

line = 'Found mode 1 of 12: EV= 1.5185449E+04, f= 19.612545, T= 0.050988.'

splitByComma=line.split(',')

fValue = splitByComma[1].replace('f= ', '').strip()
print(fValue)

结果19.612545被打印(尽管仍然是一个字符串)。

用逗号分割你的行,抓住第二块,然后打破f价值。错误检查和转换由您决定!

于 2012-07-27T18:47:30.007 回答
0

在这里使用正则表达式是疯狂的。只需按如下方式使用string.find:(其中 string 是保存您的字符串的变量的名称)

index = string.find('f=')
index = index + 2 //skip over = and space 
string = string[index:] //cuts things that you don't need 
string = string.split(',') //splits the remaining string delimited by comma
your_value = string[0] //extracts the first field

我知道它很丑,但与 RE 相比,它什么都不是。

于 2012-07-27T18:52:13.437 回答