zoukankan      html  css  js  c++  java
  • ValueError: row index was 65536, not allowed by .xls format

    报错:ValueError: row index was 65536, not allowed by .xls format

    读取.xls文件正常,在写.xls文件,pd.to_excel()时候会报错

    原因:写入的文件行数大于65536

    Pandas 读取 Excel 文件的引擎是 xlrd ,xlrd 虽然同时支持 .xlsx 和 .xls 两种文件格式,但是在源码文件 xlrd/sheet.py 中限制了读取的 Excel 文件行数必须小于 65536,列数必须小于 256。

    xlrd和xlwt处理的是xls文件,单个sheet最大行数是65535,如果有更大需要的,建议使用openpyxl函数,最大行数达到1048576。
    如果数据量超过65535就会遇到:ValueError: row index was 65536, not allowed by .xls format

    解决:

    方法1: 直接使用openpyxl

    import openpyxl
    
    def readExel():
        filename = r'D:	est.xlsx'
        inwb = openpyxl.load_workbook(filename)  # 读文件
        sheetnames = inwb.get_sheet_names()  # 获取读文件中所有的sheet,通过名字的方式
        ws = inwb.get_sheet_by_name(sheetnames[0])  # 获取第一个sheet内容
    
        # 获取sheet的最大行数和列数
        rows = ws.max_row
        cols = ws.max_column
        for r in range(1,rows):
            for c in range(1,cols):
                print(ws.cell(r,c).value)
            if r==10:
                break
    
    def writeExcel():
        outwb = openpyxl.Workbook()  # 打开一个将写的文件
        outws = outwb.create_sheet(index=0)  # 在将写的文件创建sheet
        for row in range(1,70000):
            for col in range(1,4):
                outws.cell(row, col).value = row*2  # 写文件
            print(row)
        saveExcel = "D:\test2.xlsx"
        outwb.save(saveExcel)  # 一定要记得保存
    

    方法2:Pandas 的 read_excel 方法中,有 engine 字段,可以指定所使用的处理 Excel 文件的引擎,填入 openpyxl ,再读取文件就可以了。

    import pandas as pd
    
    df = pd.read_excel(‘./data.xlsx’, engine=’openpyxl’)  

    pd.write( ,engine=’openpyxl’)
  • 相关阅读:
    php--点赞功能的实现
    php --图片加图片水印
    php--获取用户ip
    json
    js中eval()和$.parseJSON()的区别
    Js操作Select大全(取值、设置选中等等)
    phpexcel--导入excel表格
    远程服务器连接
    iis 重新安装后 重新注册asp.net
    筛选两个数组中不同的元素
  • 原文地址:https://www.cnblogs.com/zwp-627/p/11751958.html
Copyright © 2011-2022 走看看