【发布时间】:2021-06-08 08:31:51
【问题描述】:
我实际上有一个程序读取 csv 并将其插入到 xlsx 工作表中,但实际上这个程序需要太多时间来执行,我不知道我如何无法优化它,如果你有一些提示帮助请给我。
df = pd.read_csv( file, sep=';', decimal='.', encoding="utf-8", index_col=False )
df.to_excel( 'tmp.xlsx', sheet_name=action, index=None, header=True )
book = openpyxl.load_workbook( template_file )
writer = pd.ExcelWriter( template_file, engine='openpyxl' )
writer.book = book
writer.sheets = dict( ( ws.title, ws ) for ws in book.worksheets )
df.to_excel( writer, action, index=None )
writer.save()
os.remove( 'tmp.xlsx' )```
【问题讨论】:
-
通过使用模板文件,您强制 openpyxl 将所有内容加载到内存中。如果你能避免这种情况和 Pandas,事情会快很多,你似乎也不需要。
-
实际上我有 8 个 csv 文件需要加载到 xlsx 中,但每个 csv 文件都进入 xlsx 的特殊工作表,我不知道没有这些 tmp 文件我怎么做
-
在只读模式下将 csv 行与 openpyxl 行链接起来。
-
我不明白如何