1

I've got a log that looks like this:

 **  &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 

I'm trying to get the following data from this:

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
...

And I'm trying to do this with what I know in Python...Which is pretty rudimentary.

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()

I've tried some gymnastics with split and join, but this didn't really work out for me...I ultimately want to plot this similar to this question, probably (I'm guessing) with a numpy array :

testname123
  • 1,061
  • 3
  • 20
  • 43

2 Answers2

1

EDIT: Updated the question as I only just realized you were trying to use numpy to plot the results, not parse the data.

You're going to want to use a simple regex pattern to parse this log file. You can produce a list of results to do with what you please.

Here is the regex pattern than will parse out your time and fixCount into match groups:

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

Link to it in action: https://regexr.com/48ph8

Please see https://pythonicways.wordpress.com/2016/12/20/log-file-parsing-in-python/ for a good example of how to do what you want.

The solution will be something like this:

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
Julian
  • 1,078
  • 5
  • 17
  • This works, but the data is already parsed...Each entry looks like what was shown in the example data. I need to get the date and fixCount into a 2xN numpy array – testname123 Feb 20 '19 at 22:52
  • Hmm, so you are trying to turn the parsed data INTO a graph? – Julian Feb 25 '19 at 21:27
0

Assuming the log file name is 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]))

Output

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
balderman
  • 22,927
  • 7
  • 34
  • 52