.xlsx到csv,包括更改数字格式和验证数据

时间:2017-05-03 08:17:39

标签: python-2.7 loops csv utf-8 number-formatting

我遇到了一个小项目来验证.xlsx文件的数据。每列中都有不同类型的数据,从数字到仅文本,日期等。

我想要完成的是打开.xlsx文件,更改数字格式(例如:通过应用Excel-numformats(' @' for string,' dd.mm.yyyy'表示日期,' 0'表示整数,' 0.00'表示小数,等等。))。在下一步中,我通过迭代每个单元格字符来验证每列的数据(我用字典替换字符),然后将其保存为带有Utf-8编码的csv。

目前我正在使用xlrd模块使用utf-8编码(encoding_overwrite)打开.xlsx文件,然后使用xlsxwriter更改数字格式。问题是我必须将更改的数据保存为.xlsx文件,然后再次使用xlrd重新打开它,对字符进行验证,然后使用unicode csv模块将其保存为.csv。但我想跳过再次保存文件然后重新打开它的步骤。 我也已经尝试过openpyxl,但是打开" big"文件和操纵数据。

有没有办法将xlsxwriter的工作簿类的数据返回到xlrd,或者xlsxwriter是否有一种迭代其数据并更改其值的方法(我无法在两者的文档中找到任何内容)库)?或许对我的情况来说,这是一个更强大的库。

请赐教。

代码示例:

    # Opening .xlsx-File (xlrd)
    input_file = open_workbook(curr_path + "/" + filename + '.xlsx', encoding_override="cp1252")</code><br>
    # Creating the dictionaries for replace
    def create_dicts(file_flag):
        global dict_letters
        global dict_numbers
        global dict_special
        global dict_control
        global dict_greek
        if file_flag <> 'sonstige':
            dict_letters = create_dict("Buchstaben")
            dict_numbers = create_dict("Zahlen")
            dict_special = create_dict("Sonderzeichen")
            dict_control = create_dict("Kontrollzeichen")
            dict_greek = create_dict("Griechisch")
        else:
            dict_all = create_dict("All")
        print "Dicts created"
    def create_dict(ws):
        keys =[]
        values =[]
        cell = ''
        dict_xl_ws = ''
        # Either create a dictionary containing all sheets or one for each sheet (depending on the parameter ws)
        if ws == "All":
            for curr_sheet in range(dict_xl_wb.nsheets):
                dict_xl_ws = dict_xl_wb.sheet_by_index(curr_sheet)
                for curr_row in range(dict_xl_ws.nrows):
                    for curr_col in [0,1]:
                        if str(dict_xl_ws.cell_value(curr_row, curr_col)) not in skip_list:
                            if curr_col == 0:
                                keys.append(str(dict_xl_ws.cell_value(curr_row, curr_col)).upper())
                            elif curr_col == 1:
                                values.append(str(dict_xl_ws.cell_value(curr_row, curr_col)).upper())
        else:
            dict_xl_ws = dict_xl_wb.sheet_by_name(ws)
            for curr_row in range(dict_xl_ws.nrows):
                for curr_col in [0,1]:
                    if str(dict_xl_ws.cell_value(curr_row, curr_col)) not in skip_list:
                        if curr_col == 0:
                            keys.append(str(dict_xl_ws.cell_value(curr_row, curr_col)).upper())
                        elif curr_col == 1:
                            values.append(str(dict_xl_ws.cell_value(curr_row, curr_col)).upper())
        return dict(zip(keys,values))
    # Calling the create_dicts() function
    create_dicts(file_flag)
    # Creating Workbook and Worksheet from class (xlsxwriter)
    test = xlsxwriter.Workbook("test.xlsx")
    test_ws = test.add_worksheet("TEST")

    # Defining the number formats
    text_format = test.add_format({'num_format': '@'})
    integer_format = test.add_format({'num_format': '0'})
    double_format = test.add_format({'num_format': '0.00'})
    date_format = test.add_format({'num_format': 'DD.MM.YYYY'})

    # Creating dictionary (key = start column; value = end column)
    integer_dict = {0:1,4:5,8:9,16:16,19:19,25:26,28:29,33:33,35:35,42:42,44:46,48:49}
    for key,value in integer_dict.iteritems():
        # Applying the formats for each column in test_ws
        test_ws.set_column(key, value,20, integer_format)

    # After that I'd like to iterate through the data of xlsxwriter's workbook/worksheet class and change the data by replacing characters with those in the dictionaries. That part is already coded, but I need a proper library to work with

    # Creating a csv-file to write the validated data into
    output_file = codecs.open(curr_path + "/" + filename + '_OUT.csv','wb', encoding='utf-8')

1 个答案:

答案 0 :(得分:0)

正如@lenz指出的那样,XLRD有一种方法可以将单元格的值更改为excel-date。 所以我使用xlrd.xldate.xldate_as_datetime()函数来更改值。解决方案如下:

date_variable = xlrd.xldate.xldate_as_datetime(ws.cell_value(cell_row, cell_col), 0).strftime('%d.%m.%Y')

为了正确地将浮点数更改为整数,我使用了以下代码片段:

integer_variable = '{0:g}'.format(Decimal(ws.cell_value(cell_row, cell_col)))

我希望这两行可以帮助我尽可能多的帮助!