Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从日志文件中获取最后一个值_Python_Python 2.7_Extract_Extraction_Logfile - Fatal编程技术网

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

中断上述命令:

  • grep'Temp'stack.log->搜索给定日志文件中的“Temp”字符串
  • tail-4->将从上述命令输出中提取最后4条记录
  • gawk-F='{print$2}'->使用“=”作为分隔符并打印第一列, 例如: 29.0*湿度 21.0*湿度 22.0*湿度 28.0*湿度

  • gawk{print$1}->使用“space”作为分隔符并打印第一列 例如,单独: 29.0*

    21.0*

    22.0*

    28.0*

  • sed s///g->将所有“”(星号)替换为空白“”(空白)

  • 最终输出如下所示:

    29.0

    21.0

    22.0

    28.0


    您可以将其重定向到一个文件,并将其作为温度读入程序。

    如果您可以,如果其他语言也可以这样做,您可以在bash shell中使用以下命令(假设日志文件名为stack.log):

    grep'Temp'stack.log | tail-4 | gawk-F='{print$2}'| gawk'{print$1}'| sed s/*//g

    中断上述命令:

  • grep'Temp'stack.log->搜索给定日志文件中的“Temp”字符串
  • tail-4->将从上述命令输出中提取最后4条记录
  • gawk-F='{print$2}'->使用“=”作为分隔符并打印第一列, 例如: 29.0*湿度 21.0*湿度 22.0*湿度 28.0*湿度

  • gawk{print$1}->使用“space”作为分隔符并打印第一列 例如,单独: 29.0*

    21.0*

    22.0*

    28.0*

  • sed s///g->将所有“”(星号)替换为空白“”(空白)

  • 最终输出如下所示:

    29.0

    21.0

    22.0

    28.0


    您可以将其重定向到一个文件,并在程序中读取温度。

    您的尝试在哪里?我看不见他们。使用列表听起来很有希望。你的尝试在哪里?我看不见他们。使用列表听起来很有希望。这很好,但假设用户不在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
    操作。就像我说的,这取决于你的文件有多大。