Writing variable from list individually in table with corresponding value from other variable

StackOverflow https://stackoverflow.com/questions/8270524

  •  08-03-2021
  •  | 
  •  

Question

Ok, the code I have is the following:

for shidx in xrange(0, book.nsheets):
print shidx
sheet = book.sheet_by_index(shidx)
d = sheet.col_values(0,2)
D = sheet.col_values(1,2)
dim = sheet.row_values(0,2)
if shidx == 0:
    #numLine = sheet.row_values(2)
    rs = sheet.col_values(6,2)
    for i in range(4):
        BB = sheet.col_values(2 + i, 2)
        if BB != 0:
            #print repr(d).rjust(2), repr(D).rjust(3), repr(BB).rjust(4), repr(rs).rjust(5)
            file = open("C:\\calcul\\SimX18_VitesseLimite\\Documents\\ncapa-20111116\\ncapa\\resources\\output.txt", "w")
            #file.write(str(table) + '\n')
            file.write(str(d) + '\n')
            file.write(str(D) + '\n')
            file.write(str(BB) + '\n')
            file.write(str(dim) + '\n')
            file.write(str(rs) + '\n')
            file.close()

I've commented out the print towards the end as I've been trying a few different things.

My aim is to write to a text file with each line corresponding to a column in a table. My problem here is splitting the list for d, D, dim and BB. Here is what I would like to do:

The ROW of dim and COLUMN of d:

dim = [17.0, 27.0, 37.0, 47.0, u'17-47'] (see table below for the first row and to what it corresponds)
d = [0.59999999999999998, 1.0, 1.5, 2.0, 2.5, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 12.0, 15.0, 17.0, 20.0, 22.0, 25.0, 28.0, 30.0, 32.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0, 100.0, 105.0, 110.0, 120.0, 130.0, 140.0, 150.0, 160.0, 170.0, 180.0, 190.0, 200.0]

Table1 1

And the first and second ROW of BB:

BB = [0.8, 0.0, 0.0, 0.0] BB = [1.0, 0.0, 0.0, 0.0]

I want to be able to write into the text file (the equivalent of a table as below):

Table2 2

However for the third ROW of BB:

BB = [1.0, 0.0, 1.8, 0.0]

I need to be able to obtain the following (i.e. there are two 'dim' options):

Table3 3

When I say that BB isn't equal to 0.0, I want to be able to write that ONE single value for d, D, BB and dim into the table but I can't get away from all the lists...

I really hope this makes sense to someone as I'm a bit stuck!

Was it helpful?

Solution

Your requirement is eventually ascertainable after multiple readings. However your code has several problems:

The indentation as displayed is botched. Presumably you are using tabs in your editor. Don't do that with Python source files. Set it up to use 4 spaces per indent level.

You are iterating over sheets but ignoring all but the first sheet; why?

You are using d, D, and rs to refer to lists of values but not iterating over the lists.

Even if d refers to a scalar as it should, doing file.write(str(d) + '\n') etc is no way of attaining your goal of a file with 5 values per line.

Opening/closing your output file once per output line would be grossly inefficient if opened in append mode, and is grossly inefficient and completely wrong as you have it, opened in write mode -- you would only ever get the last line written.

You need to give some thought to who/what is going to read/consume your output file, and the format that they/it would prefer.

Enough of that. Here is what you ought to do (pseudocode):

open your workbook
open your output file
for each sheet:
    get dim # a list
    for each data row:
        get d, D, and rs # three scalars
        for each bb value in the BB zone of the current row:
            if bb is non-zero:
                get the corresponding dim value (e.g. 27)
                write d, D, bb, dim_value, rs as 1 line to the output file
close your output file
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top