Why is my Data Missing While Parsing & Writing to an Excel Sheet with xlwt? - Python -
i have issue parsing dictionary data excel (".xls") not values in dictionary end in excel file ("testbk1.xls"). i've been trying identify issue time & resolve seeking further insight ... ** can highlight problem is?**
expected excel output
resulting excel output
code:
import xlwt book = xlwt.workbook() sheet1 = book.add_sheet('sample1', cell_overwrite_ok=true) data = {'10.110.10.10': [('ar01', 'module', '', 'modules', 'catalyst 6500 series supervisor modules', 'ppl94941mg', '31-1-2018'), ('ar01', 'module', '', 'modules', 'tm', 'fqw10470ntf', '31-3-2018'), ('ar01', 'module', '', 'modules', 'tm', 'fns120010wa', '31-3-2018')], '110.110.1.1': [('ar02', 'module', '', 'modules', 'catalyst 6500 series supervisor modules', 'ral1138zec1', '31-1-2018'), ('ar02', 'module', '', 'modules', 'tm', 'opa10233u08', '31-3-2018'), ('ar02', 'module', '', 'modules', 'tm', 'hjg1267u9u4', '31-3-2018')], '10.110.1.97': [('eee-rt', 'module', '', 'modules', 'tm', 'wgc4556u5ua', '31-3-2018')], '10.110.1.225': [('sss-igw-rt-1', 'module', 'mm4', 'modules', 'tm', 'hjm151rty6a', '31-3-2018'), ('sss-igw-rt-1', 'module', 'mm4', 'modules', 'tm', 'tttui909e7', '31-3-2018'), ('sss-igw-rt-1', 'module', 'mm4', 'modules', 'tm', 'mmm1wef7813', '31-3-2018')], '10.110.172.2': [('sss-igw-rt-2', 'module', 'mm4', 'modules', 'tm', 'agm61cgbudu', '31-3-2018'), ('sss-igw-rt-2', 'module', 'mm4', 'modules', 'tm', 'ryuyuh3l9e9', '31-3-2018'), ('sss-igw-rt-2', 'module', 'mm4', 'modules', 'tm', 'yui23rt9ew', '31-3-2018')]} def write_to_file(): header = ['ip address', 'host name', 'equipment type', 'dc', 'product type', 'product family', 'serial number','hardware eos'] n = 0 in header: sheet1.write(0, n, i) n += 1 col, caption in enumerate(data, 1): sheet1.write(col, 0, caption) row, dt in enumerate(data[caption], 1): col_no, q in enumerate(dt): if not q: q = 'na' if col_no == 0: sheet1.write(row, col_no, caption) sheet1.write(row, col_no+1, q) else: sheet1.write(row, col_no+1, q) book.save("testbk1.xls") return write_to_file()
thanks all. found rows being overwritten because row value not changing "for row, dt in enumerate(data[caption], 1)".
i introduced "row_cnt" variable increment row number instead of using "row" index in "for row, dt in enumerate(data[caption], 1):". resolved it.!
def write_to_file(): header = ['ip address', 'host name', 'equipment type', 'dc', 'product type', 'product family', 'serial number','hardware eos'] n = 0 in header: sheet1.write(0, n, i) n += 1 row_cnt = 1 col, caption in enumerate(data, 1): sheet1.write(col, 0, caption) row, dt in enumerate(data[caption], 1): col_no, q in enumerate(dt): if not q: q = 'na' if col_no == 0: sheet1.write(row_cnt, col_no, caption) sheet1.write(row_cnt, col_no+1, q) else: sheet1.write(row_cnt, col_no+1, q) row_cnt += 1 book.save("testbk1.xls") return
Comments
Post a Comment