Read and Write data to excel sheet using python -
i need read excel file shown below, excel sheet have following columns (ip address, system name, system value, phone number, credentials) these headers. , have following entries.
ip address|system name |system value |phone number|credentials| 100.3.32.2|win32 |e2 |1234567890 |admin | 100.3.32.1|linux |e3 |1234567891 |user |
i need read excel sheet including header , respective values under header, , should write new data under respective header, , should not overwrite if new header added between existing header(if new column added)
new value , column added using python.
ip address|name|system name|system value|phone number|credentials| 100.3.32.2| |win32 |e2 |1234567890 |admin | 100.3.32.1| |linux |e3 |1234567891 |user | 100.2.2.3.| |win64 |e4 |9929123445 |admin2 | 100.2.2.3.| |win64 |e4 |9929123445 |admin2 |
this python code have written. not able write data excel sheet based on header name, data getting written based on column number, if new column added in between other columns, value written column, suppose if name
added between ip address , system name, system name
values added name
, should not happen should write data corresponding header i.e column. can please provide me input how go this.
ip,cred,sn,st,phn in list: w_sheet.write(row,0,ip) w_sheet.write(row,2, cred ) w_sheet.write(row,3, sn) w_sheet.write(row,5,st) w_sheet.write(row,4,phn) row += 1
here 0,2,3,5,4 column number, data written excel sheet based on column number. xlrd import open_workbook xlutils.copy import copy
book = open_workbook(r'testdata_new_modified.xls') sheet = book.sheet_by_index(1) wb= copy(book) w_sheet = wb.get_sheet(1) dict_list=[] keys = [sheet.cell(0, col_index).value col_index in range(sheet.ncols)] in keys: print #w_sheet.write(sheet.nrows,i,list[i]) d={} row_index in range(1, sheet.nrows): d = {keys[col_index]: sheet.cell(row_index, col_index).value col_index in xrange(sheet.ncols)} dict_list.append(d) le in range(0,len(dict_list)): data=dict_list[le][i] print data list = ( ['100.98.23.22', 'admin2', 'sn1','e1','9900'], ['100.33.44.5', 'admin3', 'sn3','e3','8888'], ['100.44.55.66', 'admin4','sn4','e4','9999']) row= sheet.nrows ip,cred,sn,st,phn in list: w_sheet.write(row,0,ip) w_sheet.write(row,2, cred ) w_sheet.write(row,3, sn) w_sheet.write(row,5,st) w_sheet.write(row,4,phn) row += 1 #to write data excel sheet wb.save('testdata_new_modified.xls')
Comments
Post a Comment