Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Concise / elegant way to reformat a set of text files?
    primarykey
    data
    text
    <p>I have written a python script to process a set of ASCII files within a given dir. I wonder if there is a more concise and/or "pythonesque" way to do it, without loosing readability?</p> <h1>Python Code</h1> <pre><code>import os import fileinput import glob import string indir='./' outdir='./processed/' for filename in glob.glob(indir+'*.asc'): # get a list of input ASCII files to be processed fin=open(indir+filename,'r') # input file fout=open(outdir+filename,'w') # out: processed file lines = iter(fileinput.input([indir+filename])) # iterator over all lines in the input file fout.write(next(lines)) # just copy the first line (the header) to output for line in lines: val=iter(string.split(line,' ')) fout.write('{0:6.2f}'.format(float(val.next()))), # first value in the line has it's own format for x in val: # iterate over the rest of the numbers in the line fout.write('{0:10.6f}'.format(float(val.next()))), # the rest of the values in the line has a different format fout.write('\n') fin.close() fout.close() </code></pre> <h1>An example:</h1> <h2>Input:</h2> <pre><code>;;; This line is the header line -5.0 1.090074154029272 1.0034662411357929 0.87336062116561186 0.78649408279093869 0.65599958665017222 0.4379879132749317 0.26310799350679176 0.087808018565486673 -4.9900000000000002 1.0890770415316042 1.0025480136545413 0.87256100700428996 0.78577373527626004 0.65539842673645277 0.43758616966566649 0.26286647978335914 0.087727357602906453 -4.9800000000000004 1.0880820021223023 1.0016316956763136 0.87176305623792771 0.78505488659611744 0.65479851808106115 0.43718526271594083 0.26262546925502467 0.087646864773454014 -4.9700000000000006 1.0870890372077564 1.0007172884938402 0.87096676998908273 0.78433753775986659 0.65419986152386733 0.4367851929843618 0.26238496225635727 0.087566540188423345 -4.9600000000000009 1.086098148170821 0.99980479337809591 0.87017214936140763 0.78362168975984026 0.65360245789061966 0.4363859610200459 0.26214495911617541 0.087486383957276398 </code></pre> <h2>Processed:</h2> <pre><code>;;; This line is the header line -5.00 1.003466 0.786494 0.437988 0.087808 -4.99 1.002548 0.785774 0.437586 0.087727 -4.98 1.001632 0.785055 0.437185 0.087647 -4.97 1.000717 0.784338 0.436785 0.087567 -4.96 0.999805 0.783622 0.436386 0.087486 </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.
 

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