Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would shorten your code to:</p> <pre><code>import sys import os from time import strftime 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 = "%s_%s.csv" % (logSys,strftime("%Y_%m_%d_ %H_%M_%S")) with open(logSys,'rb') as log, open(newLogSys,'wb') as nL: nL.writelines(lineI.replace(':' if lineI[0]=='#' else ' ', ',') for lineI in log) </code></pre> <h2>edit</h2> <p>I still don't understand what you mean by the addition of another line, that is to say '\n', to lines other than those that begin with '#'</p> <p>I ran the following code with your sample and I didn't observe something looking like what you describe. Sorry, but I can't propose any solution for a problem I don't perceive .</p> <pre><code>from time import strftime import re ss = ('--|| ||:|||:||--||| \r\n' '#10 23:30 abcdef : \r\n' '802 12:25 xyz : \r\n' '\r\n' '#:35 11:18+14:39 sunny vale : sunny sea\r\n' ' 651454451 drh:hdb 54:1\r\n' ' \r\n' ': 541514 oi:npvert654165:8\r\n' '#5415:v541564zervt\r\n' '# :: \r\n' '#::: :::\r\n' ' E\r\n') regx = re.compile('(\r?\n(?!$))|(\r?\n$)') def smartdispl(com,smth,regx = regx): print '\n%s\n%s\n%s' %\ ('{0:{fill}{align}70}'.format(' %s ' % com,fill='=',align='^'), '\n'.join(repr(el) for el in smth.splitlines(1)), '{0:{fill}{align}70}'.format('',fill='=',align='^')) logSys = 'poiu.txt' with open(logSys,'wb') as f: f.write(ss) with open(logSys,'rb') as f: smartdispl('content of the file '+logSys,f.read()) newLogSys = "%s_%s.csv" % (logSys,strftime("%Y_%m_%d_ %H_%M_%S")) with open(logSys,'rb') as log, open(newLogSys,'wb') as nL: nL.writelines(lineI.replace(':' if lineI[0]=='#' else ' ', ',') for lineI in log) with open(newLogSys,'rb') as f: smartdispl('content of the file '+newLogSys,f.read()) </code></pre> <p>result</p> <pre><code>==================== content of the file poiu.txt ==================== '--|| ||:|||:||--||| \r\n' '#10 23:30 abcdef : \r\n' '802 12:25 xyz : \r\n' '\r\n' '#:35 11:18+14:39 sunny vale : sunny sea\r\n' ' 651454451 drh:hdb 54:1\r\n' ' \r\n' ': 541514 oi:npvert654165:8\r\n' '#5415:v541564zervt\r\n' '# :: \r\n' '#::: :::\r\n' ' E\r\n' ====================================================================== ======= content of the file poiu.txt_2012_02_07_ 00_48_55.csv ======== '--||,,||:|||:||--|||,\r\n' '#10 23,30 abcdef , \r\n' '802,12:25,xyz,,:,,\r\n' '\r\n' '#,35 11,18+14,39 sunny vale , sunny sea\r\n' ',,651454451,drh:hdb,54:1\r\n' ',,,,\r\n' ':,541514,oi:npvert654165:8\r\n' '#5415,v541564zervt\r\n' '# ,, \r\n' '#,,, ,,,\r\n' ',E\r\n' ====================================================================== </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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