Python 如何从日志文件中获取最后一个值
我有一个保存温度值的日志文件。Python 如何从日志文件中获取最后一个值,python,python-2.7,extract,extraction,logfile,Python,Python 2.7,Extract,Extraction,Logfile,我有一个保存温度值的日志文件。 使用此代码,我只能从中提取温度值 代码: 我的日志文件: 2017-08-04 -> 16:14:29 Temp=28.0* Humidity=36.0% 代码输出: 28 28 25 29 28 25 我想做的是,只提取最后四个结果。 我试过使用数组和列表。但是没有结果 我错过了什么? 如何让这个程序只得到最后四个结果 提前感谢。您可以将温度保存在列表中,并使用切片获取最后4个: import re import itertools temps =
使用此代码,我只能从中提取温度值 代码: 我的日志文件:
2017-08-04 -> 16:14:29
Temp=28.0* Humidity=36.0%
代码输出:
28
28
25
29
28
25
我想做的是,只提取最后四个结果。我试过使用数组和列表。但是没有结果 我错过了什么?
如何让这个程序只得到最后四个结果
提前感谢。您可以将温度保存在列表中,并使用切片获取最后4个:
import re
import itertools
temps = []
infile = "/home/pi/Mysensor/Logs/test.log"
for line in open(infile):
match = re.search('Temp=(\d+)', line)
if match:
test = match.group(1)
temps.append(test)
print temps[:-5:-1]
要了解有关切片的更多信息,可以将温度保存在列表中,并使用切片获取最后4个:
import re
import itertools
temps = []
infile = "/home/pi/Mysensor/Logs/test.log"
for line in open(infile):
match = re.search('Temp=(\d+)', line)
if match:
test = match.group(1)
temps.append(test)
print temps[:-5:-1]
要了解有关切片的更多信息,我想这实际上取决于日志文件的大小,但我可以想出几种方法来实现这一点 最简单的方法可能是使用
deque
from collections import deque
import re
temps = deque(maxlen=4)
infile = "/home/pi/Mysensor/Logs/test.log"
with open(infile, "r") as fh:
for line in fh:
match = re.search('Temp=(\d+)', line)
if match:
temp = match.group(1)
temps.append(temp)
我想这实际上取决于日志文件的大小,但我可以想出几种方法来实现这一点 最简单的方法可能是使用
deque
from collections import deque
import re
temps = deque(maxlen=4)
infile = "/home/pi/Mysensor/Logs/test.log"
with open(infile, "r") as fh:
for line in fh:
match = re.search('Temp=(\d+)', line)
if match:
temp = match.group(1)
temps.append(temp)
一种简单的方法是使用linux shell中的tail
1 import os
2
3 def my_tail(f, n):
4 stdin, stdout = os.popen2("tail -n " + str(n) + " "+ f)
5 lines = stdout.readlines();
6 return lines
7
8 print my_tail("./my_log.txt",4)
一种简单的方法是使用linux shell中的tail
1 import os
2
3 def my_tail(f, n):
4 stdin, stdout = os.popen2("tail -n " + str(n) + " "+ f)
5 lines = stdout.readlines();
6 return lines
7
8 print my_tail("./my_log.txt",4)
如果您同意,如果其他语言也可以这样做,您可以在bash shell中使用以下命令(假设日志文件名为stack.log): grep'Temp'stack.log | tail-4 | gawk-F='{print$2}'| gawk'{print$1}'| sed s/*//g 中断上述命令:
您可以将其重定向到一个文件,并将其作为温度读入程序。如果您可以,如果其他语言也可以这样做,您可以在bash shell中使用以下命令(假设日志文件名为stack.log): grep'Temp'stack.log | tail-4 | gawk-F='{print$2}'| gawk'{print$1}'| sed s/*//g 中断上述命令:
您可以将其重定向到一个文件,并在程序中读取温度。您的尝试在哪里?我看不见他们。使用列表听起来很有希望。你的尝试在哪里?我看不见他们。使用列表听起来很有希望。这很好,但假设用户不在Windows box上。@VKShopov我在linux上,输出是
['Temp=18.0*湿度=36.0%\n','\n','\n',2017-08-04->16:14:29\n','Temp=28.0*湿度=36.0%\n']
,只是最后一行。@Sachith好的,有四个'\n'-每一个都算作新行。因此,您的每个数据记录似乎都由几行组成。这很好,但假设用户不在Windows box上。@VKShopov我在linux上,输出是['Temp=18.0*湿度=36.0%\n','\n','2017-08-04->16:14:29\n','Temp=28.0*湿度=36.0%\n']
,,只有最后一行。@Sachith嗯,有四个'\n'-每一个都算作新行。看来你的数据的每一条记录都是由几行组成的。太棒了,这就成功了。但在工作中,我关注的是python而不是shell。谢谢你的回答。太棒了,这就成功了。但在工作中,我关注的是python而不是shell。谢谢你的回答。你的答案重复了一遍,Tisp的答案给了我最好的解答。他们都重复了一遍。它们几乎是相同的代码。唯一的区别是,使用deque意味着不必将整个数组保存在内存中。缺点是您需要在每个阶段执行额外的pop
操作。就像我说的,这取决于你的文件有多大。你的答案是迭代的,Tisp的答案给了我最好的解决方案。它们都是迭代的。它们几乎是相同的代码。唯一的区别是,使用deque意味着不必将整个数组保存在内存中。缺点是您需要在每个阶段执行额外的pop
操作。就像我说的,这取决于你的文件有多大。