最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python各種excel寫入方式的速度對比

 更新時間:2020年11月10日 09:29:09   作者:左手小兜  
這篇文章主要介紹了python各種excel寫入方式的速度對比,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

經(jīng)過實驗,新建一個excel表格,該表格擁有7個sheet,每個sheet有800條數(shù)據(jù),其中最后一個sheet為空。

首先使用openpyxl進行寫入操作,代碼如下:

book = openpyxl.Workbook()
auths = Auth.objects.filter(owner_id=1)
filename = '導出數(shù)據(jù)'
for auth in auths:
  sheet = book.create_sheet(auth.name, index = 0)
  sheet.append([
      _("書名"),
      _("作者"),
      _("譯者"),
      _("出版社"),
      _("序列號"),
      _("總頁數(shù)"),
    ])
  objs = None
  objs = Book.objects.filter(owner_id=auth.id)
  for u in objs:
    data = []
    data.append(u.name)
    data.append(auth.name)
    data.append(u.translator)
    data.append(u.press)
    data.append(u.serializer)
    data.append(u.page)
    sheet.append(data)
return ExcelBookResponse(book, filename)


使用xlwt寫入數(shù)據(jù):

book = xlwt.Workbook()
auths = Auth.objects.filter(owner_id=1)
filename = '導出數(shù)據(jù)'
for auth in auths:
  sheet = book.add_sheet(sensor.name)
  sheet.write(0, 0, _("書名"))
  sheet.write(0, 1, _("作者"))
  sheet.write(0, 2, _("譯者"))
  sheet.write(0, 3, _("出版社"))
  sheet.write(0, 4, _("序列號"))
  sheet.write(0, 5, _("總頁數(shù)"))
  i = 1
  objs = None
  objs = Book.objects.filter(owner_id=auth.id)
  for u in objs:
    sheet.write(i, 0, u.name)
    sheet.write(i, 1, auth.name)
    sheet.write(i ,2,u.translator)
    sheet.write(i ,3,u.press)
    sheet.write(i, 4, u.serializer)
    sheet.write(i, 5, u.page)
    i += 1
return ExcelBookResponse(book, filename)

使用XlsxWriter寫入數(shù)據(jù):

book = xlsxwriter.Workbook(output)
auths = Auth.objects.filter(owner_id=1)
for auth in auths:
  sheet = book.add_worksheet(sensor.name)
  header = [
      _("書名"),
      _("作者"),
      _("譯者"),
      _("出版社"),
      _("序列號"),
      _("總頁數(shù)"),
    ]
  sheet.write_row("A1", header)
  objs = Book.objects.filter(owner_id=auth.id)
  i = 1
  for u in objs:
    sheet.write(i, 0, u.name)
    sheet.write(i, 1, auth.name)
    sheet.write(i ,2,u.translator)
    sheet.write(i ,3,u.press)
    sheet.write(i, 4, u.serializer)
    sheet.write(i, 5, u.page)
    i += 1
book.close()
file_ext = 'xlsx'
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
# self['Content-Disposition'] = 'attachment; filename*=UTF-8\'\'"{2}.{1}"; filename="{0}.{1}"'.format(filename.replace('"', '\"'), file_ext, urllib.parse.quote(filename.replace('"', '\"'))).encode('utf8')
return HttpResponse(content=output.getvalue(), content_type=mimetype)

三者的時間比較(兩種方式的文件內(nèi)容是一樣的):

openpyxl: 文件大小為110.75kb, 平均時間大約為570ms

xlwt: 文件大小為505.91kb,平均時間大約為440ms

XlsxWrite: 文件大小為109.28kb,平均時間大約為500ms

xlwt寫入的行數(shù)有限制,因此對于較大的文件來說,XlsxWrite的速度較快一點

補充知識:python寫入excel文件太慢如何解決-python往excel寫入大量數(shù)據(jù)

目前用的openpyxl,從數(shù)據(jù)庫獲取8W行的數(shù)據(jù)通過openpyxl寫入excel,要花費接近8分鐘,這也太慢了,用kettle的插件秒進,python有什么方法能提升速度么,或者openpyxl能批量插入么,按行效率太低了

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from openpyxl import Workbook as wbook
def xlsx(filename, rows_info, sheet='Result'):
if filename and sheet:
wb = wbook()
_sheet = wb.active
_sheet.title = sheet
row = _sheet.max_row
for line in rows_info:
if isinstance(line, str):
row_list = [line]
elif isinstance(line, dict):
row_list = list(line.values())
else:
try:
row_list = list(line)
except:
row_list = []
for col in range(0, len(row_list)):
col_info = row_list[col]
_sheet.cell(row, col + 1, col_info)
row += 1
wb.save(filename)
else:
return '文件和sheet不能為空'

以上這篇python各種excel寫入方式的速度對比就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 用Python寫一個自動木馬程序

    用Python寫一個自動木馬程序

    這篇文章主要介紹了用Python寫一個自動木馬程序的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • Python使用SQLAlchemy操作Mysql數(shù)據(jù)庫的操作示例

    Python使用SQLAlchemy操作Mysql數(shù)據(jù)庫的操作示例

    SQLAlchemy是Python的SQL工具包和對象關(guān)系映射(ORM)庫,它提供了全套的企業(yè)級持久性模型,用于高效、靈活且優(yōu)雅地與關(guān)系型數(shù)據(jù)庫進行交互,這篇文章主要介紹了Python使用SQLAlchemy操作Mysql數(shù)據(jù)庫,需要的朋友可以參考下
    2024-08-08
  • Python文件打開讀取寫入方法實用案例

    Python文件打開讀取寫入方法實用案例

    我們在工作中經(jīng)常需要用python對文本文件進行內(nèi)容的讀取和寫入,下面這篇文章主要給大家介紹了關(guān)于Python文件打開讀取寫入方法的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • Opencv判斷顏色相似的圖片示例代碼

    Opencv判斷顏色相似的圖片示例代碼

    這篇文章主要介紹了Opencv判斷顏色相似的圖片示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • 基于python實現(xiàn)操作git過程代碼解析

    基于python實現(xiàn)操作git過程代碼解析

    這篇文章主要介紹了基于python實現(xiàn)操作git過程代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • python matplotlib中文顯示參數(shù)設(shè)置解析

    python matplotlib中文顯示參數(shù)設(shè)置解析

    這篇文章主要介紹了python matplotlib中文顯示參數(shù)設(shè)置解析,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Python3.10耙梳加密算法Encryption種類及開發(fā)場景

    Python3.10耙梳加密算法Encryption種類及開發(fā)場景

    這篇文章主要為大家介紹了Python3.10加密,各種加密,耙梳加密算法Encryption種類及開發(fā)場景運用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Python3如何解決錯誤UnicodeDecodeError

    Python3如何解決錯誤UnicodeDecodeError

    當我們使用Python3來處理文本時,一個非常常見的問題就是UnicodeDecodeError,本文小編就來深入聊聊這個錯誤是怎么來的以及怎樣來解決它吧
    2025-03-03
  • 圖文詳解Python中最神秘的一個魔法函數(shù)

    圖文詳解Python中最神秘的一個魔法函數(shù)

    Python進階之路我覺得有兩個東西一定要了解,一個是魔法函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python中最神秘的一個魔法函數(shù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2021-12-12
  • Python?pyecharts數(shù)據(jù)可視化實例詳解

    Python?pyecharts數(shù)據(jù)可視化實例詳解

    PyEcharts是一個用于生成?Echarts圖表的類庫,?Python是一門富有表達力的語言,很適合用于數(shù)據(jù)處理,下面這篇文章主要給大家介紹了關(guān)于Python?pyecharts數(shù)據(jù)可視化的相關(guān)資料,需要的朋友可以參考下
    2022-05-05

最新評論

咸阳市| 东莞市| 松溪县| 穆棱市| 万宁市| 青田县| 哈尔滨市| 容城县| 嘉义县| 揭西县| 桂阳县| 龙胜| 乌审旗| 乌拉特后旗| 敦化市| 正蓝旗| 商河县| 柘城县| 栾川县| 邹平县| 鹰潭市| 亳州市| 黑山县| 双江| 平罗县| 彭水| 金山区| 阿尔山市| 天津市| 隆林| 普兰店市| 崇礼县| 怀集县| 应城市| 浑源县| 龙游县| 许昌县| 都江堰市| 伊吾县| 阜康市| 崇文区|