使用python将多个csv转换为xls,数字存储为整数而不是文本

时间:2015-09-30 03:30:27

标签: python xlwt

我正在编写一个将多个csv文件转换为xls文件的脚本,我这样做了,但我面临的问题是数字存储为文本而不是数字(整数)。 请帮助我将数字存储为数字而不是文本。

请帮助。

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")

1 个答案:

答案 0 :(得分:0)

按照您列出的代码,您将覆盖以下呼叫:

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

在你的街区末尾用这一行:

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

下面标有***的行似乎正在取消你的好工作:

           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)**