python 读取excel太慢_Python 读取excel并转换为字典
生活随笔
收集整理的這篇文章主要介紹了
python 读取excel太慢_Python 读取excel并转换为字典
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方法一:利用利用xlrd讀取excel文件
其實整個過程比較簡單,利用xlrd讀取excel文件,再把讀取到的數據轉換為dict即可。
1.安裝 xlrd
pip install xlrd2.讀取文件,并進行格式轉換
導入的excel表格的格式是這樣的:
解析后的格式為
[{'編號': 1, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}, {'編號': 2, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}, {'編號': 3, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}, {'編號': 4, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}, {'編號': 5, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}, {'編號': 6, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}]這里只寫出主要的代碼段
# excel轉dictdef excel_to_dict(self, *args, **kwargs): """ excel轉dict 1.傳來的文件可以是文件路徑,也可以是二進制文件 2.傳來的可以是二進制文件,這里以django接收前端傳來文件為例: 接收用 request.FILES.get("fileName", None) 傳入 my_file 即可 kwargs接收的參數有: _sheet索引,0代表第一個表,1代表第二個表,默認0 _max表格最大的行數,默認2000行 _min表格最小的行數,默認1行 """ # excel 文件 excel_file = self.__mark # sheet 索引 _sheet = kwargs.get("sheet", 0) # max 最大條數 _max = kwargs.get("max", 2000) # min 最小條數 _min = kwargs.get("min", 0) # 判斷是否為文件路徑 if os.path.exists(excel_file): workbook = xlrd.open_workbook(excel_file) else: # 上傳的文件不保存,直接在內存中讀取文件 workbook = xlrd.open_workbook(filename=excel_file.name, file_contents=excel_file.read()) # 根據sheet索引或者名稱獲取sheet內容 data_sheet = workbook.sheets()[_sheet] # 獲取sheet名稱,行數,列數據 sheet_name = data_sheet.name sheet_nrows = data_sheet.nrows sheet_ncols = data_sheet.ncols # 文件記錄不得大于2000條 if sheet_nrows > _max: return {"code": "0001", "msg": "文件記錄大于{}條,請聯系管理員上傳".format(_max), "data": None} # 判斷是否為空數據 if sheet_nrows <= _min: return {"code": "0001", "msg": "空數據表格,停止導入", "data": None} # excel轉dict get_data = [] for i in range(1, sheet_nrows): # 定義一個空字典 sheet_data = {} for j in range(sheet_ncols): # 獲取單元格數據類型 c_type = data_sheet.cell(i, j).ctype # 獲取單元格數據 c_cell = data_sheet.cell_value(i, j) if c_type == 2 and c_cell % 1 == 0: # 如果是整形 c_cell = int(c_cell) elif c_type == 3: # 轉成datetime對象 c_cell = datetime(*xlrd.xldate_as_tuple(c_cell, 0)).strftime('%Y-%m-%d %H:%M:%S') elif c_type == 4: c_cell = True if c_cell == 1 else False sheet_data[data_sheet.row_values(0)[j]] = c_cell # 循環每一個有效的單元格,將字段與值對應存儲到字典中 # 字典的key就是excel表中每列第一行的字段 # sheet_data[self.keys[j]] = self.table.row_values(i)[j] # 再將字典追加到列表中 get_data.append(sheet_data) # 返回從excel中獲取到的數據:以列表存字典的形式返回 return get_data方法二:利用xToolkit庫
1.安裝方法:
pip install xToolkit -i http://pypi.douban.com/simple --trusted-host pypi.douban.comxToolkit庫是我自己封裝的python內置庫的一個擴展庫.把python的datetime,string,list,dist,xthread等數據結構進行了功能的擴展。里面好用的功能比較多,可以前往 https://blog.csdn.net/qq_22409661/article/details/108531485 查看具體用法。
2.使用方法比較簡單,一行代碼即可搞定
xfile.read("./result/t_excel.xls").excel_to_dict()導入的excel表格的格式是這樣的:
# excel轉dict# 1.傳來的文件可以是文件路徑,也可以是二進制文件# 2.傳來的可以是二進制文件,這里以django接收前端傳來文件為例:# 接收用 request.FILES.get("fileName", None) 傳入 my_file 即可# kwargs接收的參數有:# _sheet索引,0代表第一個表,1代表第二個表,默認0# _max表格最大的行數,默認2000行# _min表格最小的行數,默認1行xfile.read("./result/t_excel.xls").excel_to_dict()[{'編號': 1, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}, {'編號': 2, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}, {'編號': 3, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}, {'編號': 4, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}, {'編號': 5, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}, {'編號': 6, '時間': '1988-07-21 00:00:00', '年齡': 1, '分數': 63.2, '總分': 1}]總結
以上是生活随笔為你收集整理的python 读取excel太慢_Python 读取excel并转换为字典的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机硬件的维护知识,电脑放了一年开不了
- 下一篇: python 项目实战视频_腾讯视频 P