美文网首页
python的xlrd导入excel

python的xlrd导入excel

作者: 蕴重Liu | 来源:发表于2019-07-16 16:41 被阅读0次
    # -*- coding: utf-8 -*-
    
    # @Time    : 2019/7/2 10:59
    # @Author  : liuyunqing
    import json
    import xlrd
    import time
    from utils.print_log import debug
    from pymongo import MongoClient
    
    db = MongoClient("192.168.0.199:27017").ctfs_admin
    
    
    
    def read_excel(bytes_content):
        wb = xlrd.open_workbook(file_contents=bytes_content, encoding_override="utf8")
        for sheet in wb.sheets():
            col = db.t_test_col
            start_time = time.time()
            field_name = sheet.row_values(0)
            item_list = []
            i_row = 1
            max_col = len(field_name)
            for i_row_item in range(i_row, sheet.nrows):
                i_col = 0  # 第1列开始
                item_dict = {'is_import':2, 'flg_deleted':0}
                # item_dict = {'is_import':1}
                for i_col_item in range(max_col):
                    cell = str(sheet.cell(i_row, i_col).value)
                    if 'f' in cell:
                        cell = float(cell[1:])
                    elif 'int' in cell:
                        cell = int(cell[3:])
                    elif 'str' in cell:
                        cell = cell[3:]
                    elif '{' in cell:
                        cell = json.loads(str(cell))
                    elif '[' in cell:
                        cell = str(cell[1:]).split(',')
                    item_dict[field_name[i_col]] = cell
                    i_col += 1
                i_row += 1
                item_list.append(item_dict)
                # col.insert_one(item_dict)
            debug('获取sheet后')
            start_time1 = time.time()
            debug(start_time1 - start_time)
            col.insert_many(item_list)
            debug('inserted')
            debug(time.time() - start_time1)
    
    if __name__ == '__main__':
        file = ''
        read_excel(file.read())
    

    相关文章

      网友评论

          本文标题:python的xlrd导入excel

          本文链接:https://www.haomeiwen.com/subject/opmtlctx.html