我试图从文本文件的第18行和第19行读取两个浮点数并找到它们的平均值。请只查看由#########
分隔的代码部分。
该文件有单词和数字的混合,我不知道如何只读取数字。第18行和第19行的示例如下:
TM-score= 0.27878 (if normalized by length of Chain_1)
TM-score= 0.36068 (if normalized by length of Chain_2)
def tmval(self,name,tmvalpath):
dir = tmvalpath
splits = []
avg = 0
number1 = 0
number2 = 0
for f in os.listdir(dir):
file_name2, file_ext = os.path.splitext(f)
n1,n2,align = file_name2.split('_')
n = ('{}_{}'.format(n1,n2))
if n == name:
#####################################################
with open(os.path.join(dir,f)) as file:
for i, line in enumerate(file):
if i == 17:
#retrieve data from 18th line
number1 =
elif i == 18:
# retrieve data from 19th line
number2 =
######################################################
avg = float(number1 + number2)/2
return avg
您可以使用正则表达式:
import re
with open(os.path.join(dir,f)) as file:
for i, line in enumerate(file):
if i == 17:
number1 = float(re.search(r'\d+\.\d+', line).group())
elif i == 18:
number2 = float(re.search(r'\d+\.\d+', line).group())
看起来像正则表达式的工作。
像[0-9] [。] [0-9] *之类的东西应该这样做。
蟒蛇:
import re
import numpy as np
line = 'TM-score= 0.36068 (if normalized by length of Chain_2)'
str = re.search(r'[0-9][.][0-9]*', line)
number1 = float(str.group(0))
print(number1)