Search code examples
pythonexcelwhile-loopsaving-data

How to save results for each while loop in python?


as you can see from my code there will be 150 dx value like dx(1) for j=50, dx(2) for j=51---dx(150) for j=149. I want to write all dx (upto 149) value in one column of "foobar.xls" file. Presently, I found in xls file, only the value of dx(150). so that is my problem. Sorry for improper questioning. (I`m new here).

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

Solution

  • 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