{"id":127598,"date":"2022-08-27T08:22:58","date_gmt":"2022-08-27T08:22:58","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=618493"},"modified":"2022-08-27T08:22:58","modified_gmt":"2022-08-27T08:22:58","slug":"how-to-convert-a-log-to-a-csv-file-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/08\/27\/how-to-convert-a-log-to-a-csv-file-in-python\/","title":{"rendered":"How to Convert a Log to a CSV File in Python?"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;618493&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p><em><strong>A not-so-fictious problem<\/strong>: Say, you&#8217;ve created a web application that runs on a dedicated Linux server in the <a href=\"https:\/\/blog.finxter.com\/top-13-attractive-cloud-developer-job-roles-2023\/\" data-type=\"post\" data-id=\"410160\" target=\"_blank\" rel=\"noreferrer noopener\">cloud<\/a>. Thousands of users visit your <a href=\"https:\/\/blog.finxter.com\/how-to-create-an-interactive-web-application-using-jupyter-notebook\/\" data-type=\"post\" data-id=\"24200\" target=\"_blank\" rel=\"noreferrer noopener\">web app<\/a> and suddenly &#8230; 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?<\/em> <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f92f.png\" alt=\"\ud83e\udd2f\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p class=\"has-base-background-color has-background\"><strong>First<\/strong>, don&#8217;t panic. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f6f8.png\" alt=\"\ud83d\udef8\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p>Let&#8217;s analyze your server logs!<\/p>\n<p>This article shows you <strong>how to convert your log file to a CSV file<\/strong> in Python, that you can use for further processing (e.g., in Pandas or Excel). <\/p>\n<h2>Problem Formulation by Example<\/h2>\n<p>Given a file <code>my_file.log<\/code> like this one I pulled from a real <a rel=\"noreferrer noopener\" href=\"https:\/\/www.ibm.com\/docs\/en\/zos\/2.1.0?topic=problems-example-log-file\" data-type=\"URL\" data-id=\"https:\/\/www.ibm.com\/docs\/en\/zos\/2.1.0?topic=problems-example-log-file\" target=\"_blank\">IBM server log<\/a> example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">03\/22 08:51:01 INFO :.main: *************** RSVP Agent started ***************\n03\/22 08:51:01 INFO :...locate_configFile: Specified configuration file: \/u\/user10\/rsvpd1.conf\n03\/22 08:51:01 INFO :.main: Using log level 511\n03\/22 08:51:01 INFO :..settcpimage: Get TCP images rc - EDC8112I Operation not supported on socket.\n03\/22 08:51:01 INFO :..settcpimage: Associate with TCP\/IP image name = TCPCS<\/pre>\n<p>How to convert this log file to a CSV file of the following standard comma-separated values format:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">03\/22,08:51:01,INFO,:.main: *************** RSVP Agent started ***************\n03\/22,08:51:01,INFO,:...locate_configFile: Specified configuration file: \/u\/user10\/rsvpd1.conf\n03\/22,08:51:01,INFO,:.main: Using log level 511\n03\/22,08:51:01,INFO,:..settcpimage: Get TCP images rc - EDC8112I Operation not supported on socket.\n03\/22,08:51:01,INFO,:..settcpimage: Associate with TCP\/IP image name = TCPCS\n<\/pre>\n<p>Or, here&#8217;s how that would look if you opened it with Excel:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"158\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-90-1024x158.png\" alt=\"\" class=\"wp-image-618634\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-90-1024x158.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-90-300x46.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-90-768x119.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-90.png 1082w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>Prettier, isn&#8217;t it? Unlike the first representation (log file), this CSV representation is easier to read for (most) human beings. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f916.png\" alt=\"\ud83e\udd16\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<h2>Convert Server Log to CSV with Pandas<\/h2>\n<p><strong>You can convert a <code>.log<\/code> file to a CSV file in Python in four simple steps: (1) <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/how-to-install-pandas-in-python\/\" target=\"_blank\">Install the Pandas <\/a>library, (2) import the Pandas library, (3) <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/read-a-csv-file-to-a-pandas-dataframe\/\" data-type=\"post\" data-id=\"440655\" target=\"_blank\">read the log file<\/a> as DataFrame, and (4) <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-to_csv-method\/\" data-type=\"post\" data-id=\"344277\" target=\"_blank\">write<\/a> the DataFrame to the CSV file<\/strong>.<\/p>\n<ol class=\"has-global-color-8-background-color has-background\">\n<li>(Optional in shell) <code>pip install pandas<\/code><\/li>\n<li><code>import pandas as pd<\/code><\/li>\n<li><code>df = pd.read_csv('my_file.log', sep='\\s\\s+', engine='python')<\/code><\/li>\n<li><code>df.to_csv('my_file.csv', index=None)<\/code><\/li>\n<\/ol>\n<p>Here&#8217;s a minimal example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import pandas as pd\ndf = pd.read_csv('my_file.log', sep='\\s\\s+', engine='python')\ndf.to_csv('my_file.csv', index=None)<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2139.png\" alt=\"\u2139\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Note<\/strong>: The <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-regex\/\" data-type=\"post\" data-id=\"6210\" target=\"_blank\">regular expression<\/a> <code>sep='\\s\\s+'<\/code> specifies more than one single <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/regex-special-characters-examples-in-python-re\/\" data-type=\"post\" data-id=\"6421\" target=\"_blank\">whitespace<\/a> as a separator between two CSV values. If you have a different separator string, you can define it here. <\/p>\n<p>You specify the <code>engine='python'<\/code> to tell Pandas that we want the Python regular expression engine to process the separator regular expression.<\/p>\n<p>The result of the code is the following CSV file:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"721\" height=\"343\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-89.png\" alt=\"\" class=\"wp-image-618613\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-89.png 721w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-89-300x143.png 300w\" sizes=\"auto, (max-width: 721px) 100vw, 721px\" \/><\/figure>\n<\/div>\n<p>You can use this CSV file as input for, say, an Excel sheet or Google Spreadsheet for further processing and analysis.<\/p>\n<p>This is what your log file looks converted to a CSV and imported to Excel:<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"158\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-91-1024x158.png\" alt=\"\" class=\"wp-image-618643\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-91-1024x158.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-91-300x46.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-91-768x119.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/08\/image-91.png 1082w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p>And this is how your log file looks as a <a href=\"https:\/\/blog.finxter.com\/how-to-create-a-dataframe-in-pandas\/\" data-type=\"post\" data-id=\"16764\" target=\"_blank\" rel=\"noreferrer noopener\">Pandas DataFrame<\/a>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> 03\/22 ... :.main: *************** RSVP Agent started ***************\n0 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]<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Related Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/pandas-to_csv\/\" data-type=\"post\" data-id=\"8027\">Python Pandas DataFrame <code>to_csv()<\/code><\/a><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) A not-so-fictious problem: Say, you&#8217;ve created a web application that runs on a dedicated Linux server in the cloud. Thousands of users visit your web app and suddenly &#8230; it crashes. Your users start complaining, and you lose revenue. More importantly, you bleed credibility by the hour. Your server is down, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-127598","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=127598"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127598\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}