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
#5
(Apr-06-2022, 09:14 PM)deanhystad Wrote: There must be lines with more than one ":" in it. Maybe a time?

You can tell split when to stop splitting. This tells split to stop after the first split.
a, b = map(str.strip, line.split(":", maxsplit=1))
items.append([b])

Thank you, it is working now with the following code:

with open(data, "r") as file:
    items = []
    for line in file:
        if ":" in line:
            a,b = map(str.strip, line.split(":", maxsplit=1))
            items.append(b)

new_result = items[0:4]
            
print(new_result)
Output:
['Hamilton-C1', '25455', '2020-09-16_21-12-40', '2.2.9']
After the list, I put it in a dataframe:
import pandas as pd

df = pd.DataFrame([new_result], columns=['Model', 'S/N', 'timestamp', 'SW'])

df
Output:
Model S/N timestamp SW 0 Hamilton-C1 25455 2020-09-16_21-12-40 2.2.9
Now, I have a folder with thousends of log files and will get this information and put it also in a dataframe.
How I can do this, as simple as possible?

Thanks for helping me again.
Reply


Messages In This Thread
RE: Extract parts of a log-file and put it in a dataframe - by hasiro - Apr-08-2022, 01:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  splitting a Dataframe Column in two parts nafshar 2 987 Jan-30-2023, 01:19 PM
Last Post: nafshar
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,683 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 1,039 Jan-23-2023, 04:56 AM
Last Post: deanhystad
  Save multiple Parts of Bytearray to File ? lastyle 1 973 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,296 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,117 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Extract a string between 2 words from a text file OscarBoots 2 1,899 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Extract specific sentences from text file Bubly 3 3,456 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,259 Mar-24-2021, 04:56 AM
Last Post: shantanu97
  Dataframe extract key values danipyth 0 1,688 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