Python 读取.TXT文件,如果值为真,则打印响应
我有一个像这样的TXT文件Python 读取.TXT文件,如果值为真,则打印响应,python,Python,我有一个像这样的TXT文件 [2019-04-03 13:59:15,034] | [DET ]: Detection 1 (start: 0.83, end: 1.57) [2019-04-03 13:59:15,044] | [DET ]: Detection 2 (start: 1.74, end: 6.74) [2019-04-03 13:59:15,062] | [DET ]: Detection 3 (start: 6.74, end:11.74) [2019-04-03 13:59
[2019-04-03 13:59:15,034] | [DET ]: Detection 1 (start: 0.83, end: 1.57)
[2019-04-03 13:59:15,044] | [DET ]: Detection 2 (start: 1.74, end: 6.74)
[2019-04-03 13:59:15,062] | [DET ]: Detection 3 (start: 6.74, end:11.74)
[2019-04-03 13:59:15,071] | [DET ]: Detection 4 (start:11.74, end:15.97)
[2019-04-03 13:59:15,072] | [DET ]: Detection 5 (start:16.06, end:18.61)
[2019-04-03 13:59:15,081] | [DET ]: Detection 6 (start:18.82, end:20.60)
[2019-04-03 13:59:15,090] | [DET ]: Detection 7 (start:20.61, end:22.46)
[2019-04-03 13:59:15,094] | [DET ]: Detection 8 (start:22.48, end:23.58)
[2019-04-03 13:59:15,099] | [DET ]: Detection 9 (start:23.75, end:25.61)
[2019-04-03 13:59:15,101] | [DET ]: Detection 10 (start:25.64, end:26.60)
[2019-04-03 13:59:15,102] | [DET ]: Detection 11 (start:26.61, end:27.30)
[2019-04-03 13:59:15,289] | [DET ]: Detection 12 (start:147.91, end:149.09)
[2019-04-03 13:59:15,289] | [DET ]: Detection 13 (start:149.10, end:154.10)
[2019-04-03 13:59:15,289] | [DET ]: Detection 14 (start:154.10, end:156.03)
Python是否可以读取此值并检测值中的大间隙,请参见检测11和12数字从27.30大幅跳到147.91,脚本是否可以读取此值并打印响应,例如“检测到间隙”以及持续时间
谢谢 首先,我需要模拟您的数据以模拟文件处理程序
s = """[2019-04-03 13:59:15,034] | [DET ]: Detection 1 (start: 0.83, end: 1.57)
# Omitted for brevity
[2019-04-03 13:59:15,102] | [DET ]: Detection 11 (start:26.61, end:27.30)
[2019-04-03 13:59:15,289] | [DET ]: Detection 12 (start:147.91, end:149.09)
[2019-04-03 13:59:15,289] | [DET ]: Detection 13 (start:149.10, end:154.10)
[2019-04-03 13:59:15,289] | [DET ]: Detection 14 (start:154.10, end:156.03)""".split("\n")
接下来,我需要提取起始值和结束值:
starts = []
ends = []
for line in s:
# Extract start val
start = line.split("start:")[1].strip().split(",")[0]
# Cast as float
start = float(start)
starts.append(start)
# Extract end val
end = line.split("end:")[1].strip().split(")")[0]
# Cast as float
end = float(end)
ends.append(end)
请注意,end=line.split(“end:”)[1].strip().split(”)[0]
的重要假设是字符串的格式始终正确。你可以看到我将其转换为浮动,这样我就可以将它们作为数字来处理
接下来,我将这些值与任意阈值进行比较(这里是20
):
输出:
Gap detected: [2019-04-03 13:59:15,289] | [DET ]: Detection 12 (start:147.91, end:149.09)
代码:
import re
import statistics as s
with open('name_of_the_file.txt','r') as f:
z = f.readlines()
data = []
for i in z:
temp = i.split('end')
m = re.findall(r"[-+]?\d*\.\d+|\d+", temp[1])
if m != None:
data.append(float(m[0]))
else:
print(i)
for j in range(len(data)-1):
cal = abs(data[j] - data[j+1])
if ( cal > s.mean(data)):
print(cal)
print('Gap detected at '+str(j+2))
输出:
121.79
Gap detected at 12
使用整个数组的平均值定义大步进我这样解决了问题:
import re
previous_end = -1
# Reading a file
with open('file.txt') as file:
# Iterating over each line
for line in file:
# Spliting on ":"
arr_lin = line.split(":")
# Very bad way of accessing the number just after 'start' and 'end'
# However, if the file structure is always identical, so "(start: 0.83, end: 1.57)" this will do the job
# Make sure we access the right index and convert it to float
start = float(re.findall(r"[-+]?\d*\.\d+|\d+", arr_lin[-2])[0])
end = float(re.findall(r"[-+]?\d*\.\d+|\d+", arr_lin[-1])[0])
# Checking for your gap
if (previous_end != -1):
print("Gap detected {}".format(start-previous_end))
previous_end = end
代码向我们展示了这个输出:
re.findall
使用正则表达式从字符串中提取浮点数
希望这有帮助如果你的问题只是“有可能吗?”,那么答案是肯定的。它甚至没有那么难。你能告诉我们到目前为止你试过什么吗?作为一个快速提示,我建议您看看python中的
readline()
,它允许您逐行读取文件。当您在线时,您可以访问所需的数据147.91
,并对照上一个数据27.30
(如果存在)进行检查。希望这有助于Irek,问题是,虽然我尝试了readline,但每个txt文件中的值都不同,所以我丢失了,因为它不是我每次都要查找的特定值。我会尝试在行中finstart:
和end:
,并提取这些值,这是完全可行的。因此,只要这些子字符串在数值之前,就可以提取它们。除非你说这条线可能是这样的:[2019-04-03 13:59:15289]|[DET]:检测12(开始:147.91,完成:149.09)
?
import re
previous_end = -1
# Reading a file
with open('file.txt') as file:
# Iterating over each line
for line in file:
# Spliting on ":"
arr_lin = line.split(":")
# Very bad way of accessing the number just after 'start' and 'end'
# However, if the file structure is always identical, so "(start: 0.83, end: 1.57)" this will do the job
# Make sure we access the right index and convert it to float
start = float(re.findall(r"[-+]?\d*\.\d+|\d+", arr_lin[-2])[0])
end = float(re.findall(r"[-+]?\d*\.\d+|\d+", arr_lin[-1])[0])
# Checking for your gap
if (previous_end != -1):
print("Gap detected {}".format(start-previous_end))
previous_end = end