xlwt - convert multiple csv to xls using python with number stored as integer not text -


i writing script converts multiple csv files xls file , did so, problem facing numbers stored text not numbers(integer). please me on store numbers numbers not text.

please on same.


import xlwt, csv, os  csv_folder = "d:\data/"  book = xlwt.workbook() headerstyle = xlwt.easyxf('font: bold 1; align: wrap on, vert centre, horiz center; borders: top 1, bottom 1, left 1, right 1; pattern: pattern solid, fore_color 5;') style = xlwt.easyxf('font: height 220; align: wrap on, vert centre, horiz center; borders: top 1, bottom 1, left 1, right 1;') file in os.listdir(csv_folder):     sheet = book.add_sheet(file[:-4], cell_overwrite_ok=true)     sheet.set_horz_split_pos(2)     sheet.set_vert_split_pos(1)     sheet.panes_frozen = true     sheet.remove_splits = true     sheet.col(0).width = 3333    #3333 = 1 inch     sheet.write_merge(0, 0, 0, 0, 'date', headerstyle)     sheet.write_merge(0, 0, 1, 6, 'smpp requests', headerstyle)     sheet.write_merge(0, 0, 7, 12, 'drop', headerstyle)     sheet.write_merge(0, 0, 14, 19, 'packet handler', headerstyle)     sheet.write_merge(0, 0, 20, 25, 'send sig', headerstyle)      open(csv_folder + file) filename:         reader = csv.reader(filename)         = 0         try:             row in reader:                 index, each in enumerate(row):                     if i==0:                         sheet.write(i, index, each, headerstyle)                     elif i==1:                         sheet.write(i, index, each, headerstyle)                     else:                         if >= 2:                             if each == ' ' or each == '':                                 each = 0                                 sheet.write(i, index, each, style)                              else:                                 if index > 0:                                     sheet.write(i, index, int(each.strip()))                                 else:                                     sheet.write(i, index, each.strip(), style)                         sheet.write(i, index, each, style)                  += 1         except unicodedecodeerror:             pass book.save("d:\data\output.xls") 

going code you've listed, you're overwriting call to:

sheet.write(i, index, int(each.strip())) 

at end of block line:

sheet.write(i, index, each, style) 

it's line flagged *** below looks undoing work:

           row in reader:             index, each in enumerate(row):                 if i==0:                     sheet.write(i, index, each, headerstyle)                 elif i==1:                     sheet.write(i, index, each, headerstyle)                 else:                     if >= 2:                         if each == ' ' or each == '':                             each = 0                             sheet.write(i, index, each, style)                          else:                             if index > 0:                                 sheet.write(i, index, int(each.strip()))                             else:                                 sheet.write(i, index, each.strip(), style)                     **sheet.write(i, index, each, style)** 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -