2

Hi I have the following output from a .log file

Date/Time: 2019-09-11 13:11:48
Global Freq. Table [Ticks @ 82.3045ps]
  -1215 : 56
  -1214 : 192
  -1213 : 105
  -1212 : 375
  -1211 : 230

I would like to know how can I write a python script to first read the columns read the values separated by the : and store them into separate arrays. I appreciate if you could give me a hand on that.

1
  • Try regex to extract pattern like XXX : XXX, you will get 2 groups separated by :, can you try it and update here. Commented Sep 11, 2019 at 6:09

1 Answer 1

4

Let's say you have a file (file.log) with the content:

Date/Time: 2019-09-11 13:11:48 Global Freq. Table [Ticks @ 82.3045ps] -1215 : 56 -1214 : 192 -1213 : 105 -1212 : 375 -1211 : 230
Date/Time: 2019-09-11 13:11:48 Global Freq. Table [Ticks @ 82.3045ps] -1215 : 56 -1214 : 192 -1213 : 105 -1212 : 375 -1211 : 230
Date/Time: 2019-09-11 13:11:48 Global Freq. Table [Ticks @ 82.3045ps] -1215 : 56 -1214 : 192 -1213 : 105 -1212 : 375 -1211 : 230

First you open the file:

file = open('file.log', 'r')

You read all the lines from the file, and then you close it:

lines = file.read().splitlines()
file.close()

Since the columns are clearly separated by the : character, it's simple to get each column separately:

for line in lines:
    if not line:
        continue

    columns = [col.strip() for col in line.split(':') if col]

    # do something

Now the column variable contains all the columns you needed.

Sign up to request clarification or add additional context in comments.

1 Comment

To gather all the data in the file you can add this after the columns assignment line: data.append( columns ) Then return data which will be a multidimensional array containing all the column data for each line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.