Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Replacing white spaces in a text file with a comma
    primarykey
    data
    text
    <p>I'm fairly rusty with my python and wanted to know if there was a better way or more efficient way of writing this script. </p> <p>The scripts purpose it to take a txt log and replace ' ' with replace with ',' to create a .csv.. Makes the logs a little easier to read.</p> <p>Any suggestions or advice would be appreciated. Thanks. </p> <pre><code>import sys import os import datetime t = datetime.datetime.now() ## time set to UTC zone ts = t.strftime("%Y_%m_%d_ %H_%M_%S") # Date format if len(sys.argv) != 2: # if CLi does not equal to 2 commands print print ("usage:progammename.py logname.ext") sys.exit(1) logSys = sys.argv[1] newLogSys = sys.argv[1] + "_" + ts +".csv" log = open(logSys,"r") nL = file(newLogSys ,"w") # Read from file log and write to nLog file for lineI in log.readlines(): rec=lineI.rstrip() if rec.startswith("#"): lineI=rec.replace(':',',').strip() nL.write(lineI + "\n") else: lineO=rec.replace(' ',',').strip() # nL.write(lineO + "\n") ## closes both open files; End script nL.close() log.close() =====Sample log======== #Date: 2008-04-18 15:41:16 #Fields: date time time-taken c-ip cs-username cs-auth-group x-exception-id sc-filter-result cs-categories cs(Referer) sc-status s-action cs-method rs(Content-Type) cs-uri-scheme cs-host cs-uri-port cs-uri-path cs-uri-query cs-uri-extension cs(User-Agent) s-ip sc-bytes cs-bytes x-virus-id 2012-02-02 16:19:01 14 xxx.xxx.xxx.xxx user domain\group dns_unresolved_hostname DENIED "Games" - 404 TCP_ERR_MISS POST - http updaterservice.wildtangent.com 80 /appupdate/appcheckin.wss - wss "Mozilla/4.0 (compatible; MSIE 8.0; Win32)" xxx.xxx.xxx.xxx 824 697 - </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload