Extract parts of a log-file and put it in a dataframe - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Extract parts of a log-file and put it in a dataframe (/thread-36845.html) |
Extract parts of a log-file and put it in a dataframe - hasiro - Apr-05-2022 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: 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) I want only this output: How I have to change my code?Thanks for help! RE: Extract parts of a log-file and put it in a dataframe - deanhystad - Apr-05-2022 This makes a dictionary where "Model" is a key and "Hamilton-C1" the value. You can use dictionary operations to get the keys or the values, or get the value associated with a key. with open("data.txt", "r") as file: items = {} for line in file: if ":" in line: a, b = map(str.strip, line.split(":")) items[a] = b print(items) print(*items.keys()) print(*items.values()) If you have no interest in a dictionary make items a list and append items.with open("data.txt", "r") as file: items = [] for line in file: if ":" in line: a, b = map(str.strip, line.split(":")) items.append(a) print(items)\ If you want each item in the list to be a list do this.with open("data.txt", "r") as file: items = [] for line in file: if ":" in line: a, b = map(str.strip, line.split(":")) items.append([b]) print(items) If every line in you log file is in the form "name: value" you could open this as a CSV file using ":" as the delimiter, or you could read it using pandas.
RE: Extract parts of a log-file and put it in a dataframe - hasiro - Apr-06-2022 (Apr-05-2022, 08:32 PM)deanhystad Wrote: This makes a dictionary where "Model" is a key and "Hamilton-C1" the value. You can use dictionary operations to get the keys or the values, or get the value associated with a key. Thank you for helping. For me is the Output of only the values (last solution) the best. But If I execute the code, I got the following error: How I can deal with this?
RE: Extract parts of a log-file and put it in a dataframe - deanhystad - Apr-06-2022 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]) RE: Extract parts of a log-file and put it in a dataframe - hasiro - Apr-08-2022 (Apr-06-2022, 09:14 PM)deanhystad Wrote: There must be lines with more than one ":" in it. Maybe a time? 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) 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 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. |