如何在python中保存每个while循环的结果?

时间:2016-09-08 08:29:58

标签: python excel while-loop saving-data

从我的代码中可以看出,对于j = 50,将有150 dx值,如dx(1),对于j = 49,dx(2)对于j = 51,dx(150),对于j = 149。我想在" foobar.xls"的一列中写入所有dx(最多149)值。文件。目前,我在xls文件中找到了dx(150)的值。这是我的问题。对不起,提出质疑。 (我是新来的)。

import matplotlib.pyplot as plt
import numpy as np 
import xlrd
import xlwt


wb = xlrd.open_workbook('Scatter plot.xlsx')
sh1 = wb.sheet_by_name('T180')
sh2=wb.sheet_by_name("T181")
sh3=wb.sheet_by_name("Sheet1")


x= np.array([sh1.col_values(1,start_rowx=50, end_rowx=299)])
x1= np.array([sh2.col_values(1, start_rowx=48, end_rowx=200)])

print x1

j=50

while j<200:
    y=np.array([sh1.cell_value(j,1)])
    condition = [(x1<=(sh1.cell_value(j,1)+100)) & (x1>=(sh1.cell_value(j,1)-200)) ]
    dx=y-x1[condition]
    j+=1
    print dx

workbook = xlwt.Workbook() 
sheet = workbook.add_sheet("Sheet1")

i=1
for n in dx:
    sheet.write(i, 0,n)
    i=i+1

workbook.save("foobar.xls")

1 个答案:

答案 0 :(得分:0)

Its working like this way. Thanks for our nice comments and helpful mind.

j=50
i=1
while j<300:
    x=np.array([sh1.cell_value(j,1)])
    condition = [(x1<=(sh1.cell_value(j,1)+100)) & (x1>=     (sh1.cell_value(j,1)-200)) ]
    dx=x-x1[condition]
    j+=1
       for n in dx:
        sheet.write(i, 0,n)
        i+=1
     print dx