有这样一个txt文本。数据如下:
Domain Configured
Clock freq.(MHz) Active time(seconds)
307.20 0.78
384.00 0.00
409.60 0.00
576.00 0.03
614.40 0.00
768.00 0.00
940.80 0.00
1171.20 0.80
1324.80 0.00
1401.60 0.01
Power collapse time(seconds): 14729.63
Low Power Island time(seconds): 1.30
Current core clock(MHz): 307.20
Total time(seconds): 14732.55
需要计算每行乘积之和(ΣClock freq* Active time)除以(1401.60*Total time)的值
1401.60是最后一个Clock freq
请展示python实现代码
非常感谢
with open('test.txt', 'r') as f:
lines = [line.replace('\n', '') for line in f.readlines() if line[0] in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']]
with open('test.txt', 'r') as f:
seconds = float(f.readlines()[-2].replace('\n', '').split(': ')[-1])
# seconds = float(f.readlines()[-1].replace('\n', '').split(': ')[-1])
sum = 0
for line in lines:
a, b = line.split(' ')
s = float(a) * float(b)
sum += s
final = float(lines[-1].split(' ')[0])
print('最后结果为:', sum/(seconds*final))
def main():
sum = 0
last = 0
res = 0
with open("data.txt") as f:
for line in f.readlines():
line = line.strip()
if len(line) == 0:
continue
if line.startswith("Total time"):
a = line.split(' ')
res = sum / (last * float(a[-1]))
continue
try:
a = line.split(' ')
if len(a) == 2:
f = float(a[0])
t = float(a[1])
last = f
sum += f * t
except:
pass
print(res)
if __name__ == '__main__':
main()