![]() |
|
[Tut] How to Convert a Log to a CSV File in Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Convert a Log to a CSV File in Python? (/thread-99878.html) |
[Tut] How to Convert a Log to a CSV File in Python? - xSicKxBot - 08-30-2022 How to Convert a Log to a CSV File in Python? 5/5 – (1 vote) A not-so-fictious problem: Say, you’ve created a web application that runs on a dedicated Linux server in the cloud. Thousands of users visit your web app and suddenly … it crashes. Your users start complaining, and you lose revenue. More importantly, you bleed credibility by the hour. Your server is down, so what do you do? ![]() First, don’t panic. ![]() Let’s analyze your server logs! This article shows you how to convert your log file to a CSV file in Python, that you can use for further processing (e.g., in Pandas or Excel). Problem Formulation by Example Given a file Code: my_file.logCode: 03/22 08:51:01 INFO :.main: *************** RSVP Agent started ***************
03/22 08:51:01 INFO :...locate_configFile: Specified configuration file: /u/user10/rsvpd1.conf
03/22 08:51:01 INFO :.main: Using log level 511
03/22 08:51:01 INFO :..settcpimage: Get TCP images rc - EDC8112I Operation not supported on socket.
03/22 08:51:01 INFO :..settcpimage: Associate with TCP/IP image name = TCPCSHow to convert this log file to a CSV file of the following standard comma-separated values format: Code: 03/22,08:51:01,INFO,:.main: *************** RSVP Agent started ***************
03/22,08:51:01,INFO,:...locate_configFile: Specified configuration file: /u/user10/rsvpd1.conf
03/22,08:51:01,INFO,:.main: Using log level 511
03/22,08:51:01,INFO,:..settcpimage: Get TCP images rc - EDC8112I Operation not supported on socket.
03/22,08:51:01,INFO,:..settcpimage: Associate with TCP/IP image name = TCPCSOr, here’s how that would look if you opened it with Excel: ![]() Prettier, isn’t it? Unlike the first representation (log file), this CSV representation is easier to read for (most) human beings. ![]() Convert Server Log to CSV with Pandas You can convert a Code: .log
Here’s a minimal example: Code: import pandas as pd
df = pd.read_csv('my_file.log', sep='\s\s+', engine='python')
df.to_csv('my_file.csv', index=None) Note: The regular expression Code: sep='\s\s+'You specify the Code: engine='python'The result of the code is the following CSV file: ![]() You can use this CSV file as input for, say, an Excel sheet or Google Spreadsheet for further processing and analysis. This is what your log file looks converted to a CSV and imported to Excel: ![]() And this is how your log file looks as a Pandas DataFrame: Code: 03/22 ... :.main: *************** RSVP Agent started ***************
0 03/22 ... :...locate_configFile: Specified configuration... 1 03/22 ... :.main: Using log level 511 2 03/22 ... :..settcpimage: Get TCP images rc - EDC8112I O... 3 03/22 ... :..settcpimage: Associate with TCP/IP image na... [4 rows x 4 columns] Related Tutorial: Python Pandas DataFrame Code: to_csv() |