Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert a Log to a CSV File in Python?
1
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? [Image: 1f92f.png]

First, don’t panic. [Image: 1f6f8.png]

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.log
like this one I pulled from a real IBM server log example:

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 = TCPCS

How 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 = TCPCS

Or, here’s how that would look if you opened it with Excel:

[Image: image-90-1024x158.png]

Prettier, isn’t it? Unlike the first representation (log file), this CSV representation is easier to read for (most) human beings. [Image: 1f916.png]

Convert Server Log to CSV with Pandas

You can convert a
Code:
.log
file to a CSV file in Python in four simple steps: (1) Install the Pandaslibrary, (2) import the Pandas library, (3) read the log file as DataFrame, and (4) write the DataFrame to the CSV file
.

  1. (Optional in shell)
    Code:
    pip install pandas

  2. Code:
    import pandas as pd

  3. Code:
    df = pd.read_csv('my_file.log', sep='\s\s+', engine='python')

  4. Code:
    df.to_csv('my_file.csv', index=None)

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)

[Image: 2139.png] Note: The regular expression
Code:
sep='\s\s+'
specifies more than one single whitespace as a separator between two CSV values. If you have a different separator string, you can define it here.

You specify the
Code:
engine='python'
to tell Pandas that we want the Python regular expression engine to process the separator regular expression.

The result of the code is the following CSV file:

[Image: image-89.png]

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:

[Image: image-91-1024x158.png]

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]

[Image: 1f30d.png] Related Tutorial: Python Pandas DataFrame
Code:
to_csv()
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: