Python Forum
Extract parts of a log-file and put it in a dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract parts of a log-file and put it in a dataframe
#1
Hi

I'm a python beginner and would like to extract parts of a log-file and put it in a dataframe.
I tryed something, but it is not what I want.

Log-File content looks like this:

Output:
---------------------------------------------------------- Model: Hamilton-C1 S/N: 25576 Export timestamp: 2020-09-17_11-03-40 SW-Version: 2.2.9
I want extract only Hamilton-C1, 25576, 2020-09-17_11-03-40, 2.2.9

#list
result = []

#function
def appendlines(line, result, word):
  if line.startswith(word):
    del result[:]
  result.append(line)
  return line, result

with open(file, "r") as lines: 
  for line in lines:              
    appendlines(line, result, ":")
new_result = [line.split() for line in result[1:5]]

print(new_result)
Output:
[['Model:', 'Hamilton-C1'], ['S/N:', '25455'], ['Export', 'timestamp:', '2020-09-16_21-12-40'], ['SW-Version:', '2.2.9']]
I want only this output:
Output:
[['Hamilton-C1'], ['25455'], ['2020-09-16_21-12-40'], ['2.2.9']]
How I have to change my code?

Thanks for help!
buran write Apr-06-2022, 09:13 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply


Messages In This Thread
Extract parts of a log-file and put it in a dataframe - by hasiro - Apr-05-2022, 06:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  splitting a Dataframe Column in two parts nafshar 2 948 Jan-30-2023, 01:19 PM
Last Post: nafshar
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,395 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 981 Jan-23-2023, 04:56 AM
Last Post: deanhystad
  Save multiple Parts of Bytearray to File ? lastyle 1 939 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,263 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,082 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Extract a string between 2 words from a text file OscarBoots 2 1,866 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Extract specific sentences from text file Bubly 3 3,399 May-31-2021, 06:55 PM
Last Post: Larz60+
  Add a new column when I extract each sheet in an Excel workbook as a new csv file shantanu97 0 2,227 Mar-24-2021, 04:56 AM
Last Post: shantanu97
  Dataframe extract key values danipyth 0 1,659 Feb-07-2021, 03:52 PM
Last Post: danipyth

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020