import xlrd book = xlrd.open_workbook('D:\Scripts\UpdateID01.xls') sheet = book.sheet_by_index(0) myList = [] for i in range(1,sheet.nrows): myList.append(sheet.row_values(i)) #print myList outFile = open('D:\Scripts\update.txt', 'wb') for i in myList: outFile.write("\nUPDATE MyTable SET LegacyID = '%s' WHERE CurrentID = '%s'" %( int(i[1]), str(i[0])))
The second sheet had white spaces at the end of CurrentID values and the update statements will fail if they are not removed. To deal with it, use str.replace method.
import xlrd book = xlrd.open_workbook('D:\Scripts\UpdateID01.xls') sheet = book.sheet_by_index(1) myList = [] for i in range(1,sheet.nrows): myList.append(sheet.row_values(i)) #print myList outFile = open('D:\Scripts\updatemyesi.txt', 'wb') for i in myList: i[0] = str(i[0]).replace(' ', '') outFile.write("\nUPDATE MyTable SET LegacyID = 0 WHERE CurrentID = '%s'" %(str(i[0])))
No comments:
Post a Comment