简体   繁体   中英

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

I am writing a script which converts multiple csv files into xls file and i did it so, but the problem i am facing is the numbers are stored as text not as numbers(integer). Please help me on this to store numbers as numbers not as text.

please help on the 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;')
for 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 to Sig', headerStyle)

    with open(csv_folder + file) as filename:
        reader = csv.reader(filename)
        i = 0
        try:
            for row in reader:
                for 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 i >= 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)

                i += 1
        except UnicodeDecodeError:
            pass
book.save("D:\data\Output.xls")

Going by the code you've listed, you're overwriting the call to:

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

at the end of your block with this line:

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

It's the line flagged with *** below that looks to be undoing your good work:

           for row in reader:
            for 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 i >= 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)**

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM