如何解析此日志以获取日期/超时以使用Python3进行绘图

时间:2019-02-20 20:25:51

标签: python numpy datetime parsing timestamp

我有一个看起来像这样的日志:

 **  &nbspWed; Feb 20 2019 at 12:38:10:734 PM :  ** **  &nbspGnssLocationListener; \- 41** \- onSatelliteStatusChanged() : fixCount = 7                                                                                          
 **  &nbspWed; Feb 20 2019 at 12:38:12:742 PM :  ** **  &nbspGnssLocationListener; \- 41** \- onSatelliteStatusChanged() : fixCount = 7                                                                                          
 **  &nbspWed; Feb 20 2019 at 12:38:14:721 PM :  ** **  &nbspGnssLocationListener; \- 41** \- onSatelliteStatusChanged() : fixCount = 7                                                                                          
 **  &nbspWed; Feb 20 2019 at 12:38:16:777 PM :  ** **  &nbspGnssLocationListener; \- 41** \- onSatelliteStatusChanged() : fixCount = 7                                                                                          
 **  &nbspWed; Feb 20 2019 at 12:38:18:729 PM :  ** **  &nbspGnssLocationListener; \- 41** \- onSatelliteStatusChanged() : fixCount = 7                                                                                           
 **  &nbspWed; Feb 20 2019 at 12:38:20:700 PM :  ** **  &nbspGnssLocationListener; \- 41** \- onSatelliteStatusChanged() : fixCount = 7                                                                                           
 **  &nbspWed; Feb 20 2019 at 12:38:22:697 PM :  ** **  &nbspGnssLocationListener; \- 41** \- onSatelliteStatusChanged() : fixCount = 7                                                                                           
 **  &nbspWed; Feb 20 2019 at 12:38:24:706 PM :  ** **  &nbspGnssLocationListener; \- 41** \- onSatelliteStatusChanged() : fixCount = 7                                                                                           
 **  &nbspWed; Feb 20 2019 at 12:38:26:783 PM :  ** **  &nbspGnssLocationListener; \- 41** \- onSatelliteStatusChanged() : fixCount = 7 

我正在尝试从中获取以下数据:

12:38:10 PM , 7
12:38:12 PM , 7
12:38:14 PM , 7
12:38:16 PM , 7
12:38:18 PM , 7
...

我正在尝试用我在Python中了解的信息来做...这很简单。

import matplotlib.pyplot as plt
import matplotlib.dates as md
import numpy as np
import datetime as dt
import time
import csv

data = []
datafile = open('fix_count_02-20-2019-day.txt' , 'r')
datareader = csv.reader((x.replace('\0','') for x in datafile), delimiter=':')
for row in datareader:
        data.append(row)


np_data = np.asarray(data)
print(np_data)

plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=25 )
ax=plt.gca()
#xfmt = md.DateFormatter('%H:%M:%S')
#ax.xaxis.set_major_formatter(xfmt)
plt.plot(np_data)

plt.show()

我已经尝试过使用splitjoin进行一些体操运动,但这对我来说并没有真正的作用...我最终想绘制一个类似于this的问题,可能(我猜)有一个numpy数组:

2 个答案:

答案 0 :(得分:1)

编辑:更新了问题,因为我只是意识到您正在尝试使用numpy绘制结果,而不是解析数据。

您将要使用简单的正则表达式模式来解析此日志文件。您可以根据自己的喜好生成结果列表。

以下是正则表达式模式,它将解析出您的时间和fixCount进入匹配组:

.*((?:\d{2}:){3}\d{3} (?:PM|AM)).*fixCount = (\d+)

链接到运行中的https://regexr.com/48ph8

请参阅https://pythonicways.wordpress.com/2016/12/20/log-file-parsing-in-python/,了解如何做自己想做的一个很好的例子。

解决方案将如下所示:

import re

log_file_path = 'fix_count_02-20-2019-day.txt'
regex = r'.*((?:\d{2}:){3}\d{3} (?:PM|AM)).*fixCount = (\d+)'

match_list = []
with open(log_file_path, 'r') as file:
    data = f.read()
    for match in re.finditer(regex, data, re.S):
        match_text = match.group(0), match.group(1)
        match_list.append(match_text)
        print match_text

# do something with match_list here

答案 1 :(得分:0)

假设日志文件名为log.txt

with open('log.txt', 'r') as log:
    lines = log.readlines()
    for line in lines:
        line = line.strip()
        print('{} {} , {}'.format(line[29:37], line[42:44], line[-1]))

输出

12:38:10 PM , 7
12:38:12 PM , 7
12:38:14 PM , 7
12:38:16 PM , 7
12:38:18 PM , 7
12:38:20 PM , 7
12:38:22 PM , 7
12:38:24 PM , 7
12:38:26 PM , 7