我有一个看起来像这样的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: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的数字有较大的跳跃,脚本可以读取此值并打印响应,例如“持续时间?
谢谢!
答案 0 :(得分:0)
首先,我需要模拟您的数据以模仿文件处理程序。
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
):
for i in range(1,len(starts)):
if starts[i]-ends[i-1] > 20:
print("Gap detected: {}".format(s[i]))
else:
pass
输出:
Gap detected: [2019-04-03 13:59:15,289] | [DET ]: Detection 12 (start:147.91, end:149.09)
答案 1 :(得分:0)
代码:
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
使用整个数组的均值定义较大步长
答案 2 :(得分:0)
我这样解决了这个问题:
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
使用正则表达式从字符串中提取浮点数。
希望有帮助