【发布时间】:2022-01-20 10:01:25
【问题描述】:
我有一个程序可以保存一个包含多个值和文本的 .txt 日志。每次运行程序时,这些时间值可能不同,但它们在此 .txt 文件中的位置始终相同。我需要获取这些值并检查它们是否小于 6 秒。我试图将我的 .txt 文件转换为字符串,然后使用:
x = string(filename[50:55])
float(x)
但它似乎不起作用。如何从 .txt 文件中的固定位置提取值,然后将它们转换为浮点数?
//编辑:
//EDIT2:
另一张日志的照片,我将如何提取这些百分比,必须低于 70 才能通过。
with open(r'C:\Users\Time_Log.txt') as f:
print(f)
lines = f.readlines()
print(r'Lines: ')
print(lines)
print(r'Type of lines:', type(lines))
# start at 1 to avoid the header
for line in range(1, len(lines)):
print(r'Type of line:', type(line))
splits = line.split("|")
print(r'Type of splits:', type(splits))
t = splits[3].split(".")
if t[0] < 6:
print(r'Test completed. Startup time is shorter than 6 seconds')
【问题讨论】:
-
浮动的位置总是一样的吗?否则,您应该查看正则表达式。您可以编辑您的帖子并添加一些输入文件的行吗?
-
请贴一个日志文件的例子,否则任何人都很难正确回答
-
照片已添加,请查看。
标签: python string type-conversion txt