0

the goal of my still very simple code is to read in a logfile and store it in a dictionary. My logfile looks like this:

Picture of my logfile

Here you can see the code of my class LogfileHandler. This class simply takes a path where the logfile is stored and converts it into a dictionary.

import jsonpickle

class: LogfileHandler
   def __init__(self, filename):
       self.lines = self.read_file(filename)
       self.log_msg_list = list(self.decode_log_file_lines(self.lines))
    
   def read_file(self, filename):
       try:
           f = open(filename, 'r')
           print('Read from file %s' % filename)
           lines = f.readlines()
           f.close()
       except:
           print('File %s does not exist' % filename)
       return lines

   def decode_log_file_lines(self, lines):
       for line in lines:
           message = jsonpickle.decode(line)
           yield message

If I now run this class in a jupyter notebook, this works without any problem: Picture of my jupyter notebook

However, if I execute this via the CMD with the following main method I get the following error and I don't understand why (It is definitely the same file I´m reading from).

#--- This is the main which I run via the cmd ---

from LogfileHandler import LogfileHandler

if __name__ == "main":
   log_file_handler = LogfileHandler('data\log_file.log')
   print(log_file_handler.log_msg_list)

This is the cmd Error output

Many thanks in advance

  • Most likely has to do with the leading zeroes in your JSON integers. A leading zero in JSON denotes an octal number. – iced Sep 01 '22 at 16:20
  • since the text in the logfile is short, it would help users if you posted the text in place of a picture. – D.L Sep 01 '22 at 16:22
  • Looks to me like the issue is a different version of Python. Try to activate your environment from which your Jupyter server runs in command prompt, and then run the script? – Josh Friedlander Sep 01 '22 at 19:41

0 Answers0