Python 将 csv 转换为 xlsx [英] Python convert csv to xlsx
本文介绍了Python 将 csv 转换为 xlsx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这篇文章中有一个 Python 示例从 csv 转换为 xls.
In this post there is a Python example to convert from csv to xls.
但是,我的文件有超过 65536 行,所以 xls 不起作用.如果我将文件命名为 xlsx,它并没有什么区别.有没有可以转换成xlsx的Python包?
However, my file has more than 65536 rows so xls does not work. If I name the file xlsx it doesnt make a difference. Is there a Python package to convert to xlsx?
推荐答案
以下是使用 xlsxwriter 的示例:
Here's an example using xlsxwriter:
import os
import glob
import csv
from xlsxwriter.workbook import Workbook
for csvfile in glob.glob(os.path.join('.', '*.csv')):
workbook = Workbook(csvfile[:-4] + '.xlsx')
worksheet = workbook.add_worksheet()
with open(csvfile, 'rt', encoding='utf8') as f:
reader = csv.reader(f)
for r, row in enumerate(reader):
for c, col in enumerate(row):
worksheet.write(r, c, col)
workbook.close()
仅供参考,还有一个名为 openpyxl 的包,可以读/写 Excel 2007 xlsx/xlsm 文件.
FYI, there is also a package called openpyxl, that can read/write Excel 2007 xlsx/xlsm files.
希望有所帮助.
这篇关于Python 将 csv 转换为 xlsx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文