create a loop inside a loop in python

i found the solution thank you guys for showing intrest, accully the function where() do the iteration it self (that i did’nt know) there is no need to put it in an other loop, only the loop over “ngrains will do the trick. data=loadtxt(‘data.csv’,delimiter=”,”) data1=data.transpose() ngrains=loadtxt(‘nombre_grain.csv’,delimiter=”,”) phi0ex=len(ngrains)*[zeros(shape(250))] for k in range(len(ngrains)): print ngrains[k] phi0ex[k]=where(data1==ngrains[k],1,0) print … Read more

Connecting data in python to spreadsheets

Are you trying to write your dictionary in a Excel Spreadsheet? In this case, you could use win32com library: import win32com.client xlApp = win32com.client.DispatchEx(‘Excel.Application’) xlApp.Visible = 0 xlBook = xlApp.Workbooks.Open(my_filename) sht = xlBook.Worksheets(my_sheet) row = 1 for element in dict.keys(): sht.Cells(row, 1).Value = element sht.Cells(row, 2).Value = dict[element] row += 1 xlBook.Save() xlBook.Close() Note that … Read more

Update a CSV file?

You can effectively update any existing file “in-place” by first giving it a temporary file name and then writing a new file with its original name. import csv import os filename=”mytestfile.csv” tempfilename = os.path.splitext(filename)[0] + ‘.bak’ try: os.remove(tempfilename) # delete any existing temp file except OSError: pass os.rename(filename, tempfilename) with open(filename, mode=”wb”) as outfile: csvwriter … Read more