如何解析此日志以获取使用Python3打印的日期/超时

2024-04-20 04:15:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

 **  &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,但这对我来说并没有真正起作用…我最终想用numpy数组来描绘这个类似于this的问题:


Tags: importnumpydatamatplotlibasnppltmd
2条回答

编辑:更新了这个问题,因为我刚刚意识到您试图使用numpy来绘制结果,而不是解析数据。你知道吗

您需要使用一个简单的regex模式来解析这个日志文件。你可以制作一个结果列表来做你想做的事。你知道吗

下面是regex模式,它将解析出您的时间并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

假设日志文件名为日志.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

相关问题 更多 >