Link to home
Start Free TrialLog in
Avatar of mgjust
mgjust

asked on

Need specific spacing in output from python script

Below is the script that I got from: https://www.experts-exchange.com/questions/23561733/Copy-text-to-new-files-with-counter-and-text-insertion-Python.html 

It was working fine, initially but now I have discovered a problem and I need some additional functionality.

I have discovered the output and the the original file I was trying to emulate are different:
aI need the following problems to be fixed:

Problem:
The Spaces Between the values to be inserted are also specific:
For example from three outputs:

from output for 001:

RGNVAL          REGION NAME    # CELLS      BEGIN        END      BEGIN        END
     1             Region 1     294276     138334     119057      92883     112160

5 spaces (before 1), 13 spaces (between 1 and Region 1), 5 spaces (between Region 1 and 1st value), 5 spaces (between 1st and 2nd value), 5 spaces (between 2nd and 3rd value), 6 spaces (between 3rd and 4th value), 5 spaces (between 4th and 5th value)

from output 064:

RGNVAL          REGION NAME    # CELLS      BEGIN        END      BEGIN        END
     1             Region 1     294276      11690         83     219419     231026

5 spaces, 13 spaces, 5 spaces, 6 spaces, 9 spaces, 5 spaces, 5 spaces

from output 074:

RGNVAL          REGION NAME    # CELLS      BEGIN        END      BEGIN        END
     1             Region 1     294276     174357     148695      56763      82425

5 spaces, 13 spaces, 5 spaces, 5 spaces, 5 spaces, 6 spaces, 6 spaces

It appears that the number of spaces depends on the next number in the line so if the next number has 6 digits, the number of spaces is 5, if it has 5 digits the number of spaces is 6, if the number has 2 digits the number of spaces is 9, etc

It looks the spacing is correct from the rest of the output.


Thanks & I always appreciate annotated code,
MJ

import csv
 
data = """\
***************************************************************
* THIS FILE CONTAINS THE VALUES OF THE PARAMETERS REQUIRED    *
* FOR THE GEOMOD MODULE, WHICH AUTOMATICALLY IS GENERATED IN  *
* IDRISI. STRONGLY RECOMMEND REEDITING IN IDRISI IF NECESSARY.*
***************************************************************
 
TIME TO BEGIN GEOMOD, INCLUSIVE         :    2001
TIME TO END GEOMOD, INCLUSIVE           :    2016
TIME STEP IN TIME UNITS                 :    1
 
# NEIGHBORS AWAY TO SEARCH,0=NO NIBBLE  :    1
WRITE DEBUG OUTPUT TO LOG FILE,YES=1    :    0
NAME OF STRATA/MASK IMAGE               :    N/A
NAME OF INITIAL LANDUSE IMAGE           :    gr_lc_01b_%03d_fp
DO ENVIR. IMPACT ANALYSIS? 1=YES, 0=NO  :    0
CMP/READ SUITABILITY SCORES(0=CP,1=RD)  :    1
NUMBER OF RUNS ONCE                     :    1
SUITABILITY IMAGE FOR SIMULATION 1      :    c2_mce_%03d
DO VALIDATION ANALYSIS? 1=YES, 0=NO     :    0
SINCE NO VALIDATION IMAGE, LANDUSE CHANGE INFO READ FROM FOLLOWS:
                                  LANDUSE STATE 1       LANDUSE STATE 2
RGNVAL          REGION NAME    # CELLS      BEGIN        END      BEGIN        END
     1             Region 1    %s        %s     %s      %s      %s
 
# OF TIMES OF OUTPUT BESIDE END TIME    :    0
NAME OF OUTPUT LANDUSE IMAGE            :    %s
"""
 
fin = open("c:\\temp\\temp2\\spreadsheet.csv", "rb")  # must be open in binary mode
reader = csv.reader(fin)                 # open reader -- it will parse the csv lines
cnt = 0
 
for cell in reader:                      # through all lines in spreadsheet
    cnt += 1                             # increment the counter
    filename = "output_%03d.gmd" % cnt   # build the filename
    fout = open(filename, "w")           # open the output file for writing
    fout.write(data % (cnt, cnt, 
                       cell[0], cell[1], cell[2], cell[3], cell[4], 
                       filename))
    fout.close()    # close the output file after writing the content
 
fin.close()         # close the spreadsheet

Open in new window

geo-071.txt
Avatar of pepr
pepr

Modify your data template by adding the width to the  %s marks. Possibly change the marks to %d for numbers.

See the snippet below. It is simplified but you should get the idea. The %5d means formatting the number to the width of 5 characters. If you add '-' just after % sign, then the resulting string will be aligned to the left.
print '%5d%20s%3d' % (1, 'region 1', 2)
print '%-5d%20s%3d' % (1, 'region 1', 2)
print '%5d%-20s%3d' % (1, 'region 1', 2)
print '%5d%20s%-3d' % (1, 'region 1', 2)

Open in new window

When running the above example in cmd window, you will get result like this...
C:\tmp\Python>c.py
    1            region 1  2
1                region 1  2
    1region 1              2
    1            region 12

Open in new window

Avatar of mgjust

ASKER

Hello,
What does the 'd' mean?  What is the '20' in front of the 's' mean?  So I think there are 11 spaces for each number meaning that if the number has:  

7 digits it will have 4 spaces before it
6 = 5 spaces
5 = 6 spaces
4 = 7 spaces
3 = 8 spaces
2 = 9 spaces
1 = 10 spaces

Thanks,
MJ
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mgjust

ASKER

Thanks again.