Advertisement

07.17.2008 at 01:20PM PDT, ID: 23574794
[x]
Attachment Details

Need specific spacing in output from python script

Asked by mgjust in Python Scripting Language, Windows Batch Scripting

Below is the script that I got from: http://www.experts-exchange.com/Q_23561733.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
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
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
Attachments:
 
correctly spaced output .gmd changed to .txt
 
[+][-]07.17.2008 at 02:06PM PDT, ID: 22030329

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.17.2008 at 02:07PM PDT, ID: 22030340

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.17.2008 at 02:17PM PDT, ID: 22030454

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.18.2008 at 11:35AM PDT, ID: 22038511

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Python Scripting Language, Windows Batch Scripting
Sign Up Now!
Solution Provided By: pepr
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07.18.2008 at 11:37AM PDT, ID: 22038529

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628