2

好的,我的代码如下:

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()

因为我一直在尝试一些不同的东西,所以我已经在最后评论了印刷品。

我的目标是写入一个文本文件,其中每一行对应于表中的一列。我的问题是拆分 d、D、dim 和 BB 的列表。这是我想做的事情:

dim 的 ROW 和 d 的 COLUMN:

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]

表格1 1

以及BB的第一和第二ROW:

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

我希望能够写入文本文件(相当于下表):

表2 2

但是对于 BB 的第三行:

BB = [1.0, 0.0, 1.8, 0.0]

我需要能够获得以下内容(即有两个“暗淡”选项):

表3 3

当我说 BB 不等于 0.0 时,我希望能够将 d、D、BB 和 dim 的 ONE 单个值写入表中,但我无法摆脱所有列表......

我真的希望这对某人有意义,因为我有点卡住了!

4

1 回答 1

0

在多次阅读之后,您的要求最终是可以确定的。但是您的代码有几个问题:

显示的缩进是拙劣的。大概您正在编辑器中使用选项卡。不要对 Python 源文件这样做。将其设置为每个缩进级别使用 4 个空格。

您正在迭代工作表,但忽略了除第一张工作表之外的所有工作表;为什么?

您正在使用d,Drs来引用值列表,但不遍历列表。

即使d它应该引用一个标量,做file.write(str(d) + '\n')etc 也无法实现每行 5 个值的文件的目标。

如果在附加模式下打开/关闭输出文件一次,效率会非常低,并且在写入模式下打开时效率非常低且完全错误——你只会写最后一行。

您需要考虑谁/什么将读取/使用您的输出文件,以及他们/它喜欢的格式。

够了。这是你应该做的(伪代码):

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
于 2011-11-26T02:56:56.683 回答