本文介绍了使用Python 3将大型CSV文件转换为Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码隐秘CSV文件到.xlsx
文件,对于小尺寸CSV文件,此代码工作正常,但是当我尝试使用大尺寸CSV文件时,显示错误.
this is my code covert CSV file to .xlsx
file, for small size CSV file this code is working fine, but when I tried for larger size CSV files, Its shows an error.
import os
import glob
import csv
from xlsxwriter.workbook import Workbook
for csvfile in glob.glob(os.path.join('.', 'file.csv')):
workbook = Workbook(csvfile[:-4] + '.xlsx')
worksheet = workbook.add_worksheet()
with open(csvfile, 'r', 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()
错误是
File "CsvToExcel.py", line 12, in <module>
for r, row in enumerate(reader):
_csv.Error: field larger than field limit (131072)
Exception ignored in: <bound method Workbook.__del__ of
<xlsxwriter.workbook.Workbook object at 0x7fff4e731470>>
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/xlsxwriter/workbook.py", line
153, in __del__
Exception: Exception caught in workbook destructor. Explicit close() may be
required for workbook.
推荐答案
我发现了带有panda包的新代码,该代码现在可以正常工作
I found Out New Code with panda package, this code is working fine now
import pandas
data = pandas.read_csv('Documents_2/AdvMedcsv.csv')
data = data.groupby(lambda x: data['research_id'][x]).first()
writer = pandas.ExcelWriter('Documents_2/AdvMed.xlsx',engine='xlsxwriter')data.to_excel(writer)
writer.save()
这篇关于使用Python 3将大型CSV文件转换为Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!