zrmxxh 2023-02-20 14:22 采纳率: 100%
浏览 29
已结题

pandas.error

I created the test.xlsx with xlwt and wanted to add sheet using the

def write(pureList,resultList,outPath,sheetName):
    d = {'words':pureList,'translated':resultList}
    df = pd.DataFrame(data=d)
    with pd.ExcelWriter(outPath,mode='a',engine="openpyxl") as writer:
        df.to_excel(writer,sheet_name=sheetName)

And here is the Error:

Traceback (most recent call last):
  File "d:\ori-desktop\exe\python\pandas\21excelwrite.py", line 25, in <module>
    write(pureList,resultList,outPath,sheetName)
  File "d:\ori-desktop\exe\python\pandas\21excelwrite.py", line 14, in write
    with pd.ExcelWriter(outPath,mode='a',engine="openpyxl") as writer:
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_openpyxl.py", line 73, in __init__
    self._book = load_workbook(self._handles.handle, **engine_kwargs)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
    self.archive = _validate_archive(fn)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\reader\excel.py", line 96, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python310\lib\zipfile.py", line 1267, in __init__
    self._RealGetContents()
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python310\lib\zipfile.py", line 1334, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

I want to know how to deal this.

  • 写回答

5条回答 默认 最新

  • CodeBytes 2023-02-20 14:31
    关注

    该回答引用ChatGPT

    The error message suggests that the file specified in outPath is not a valid zip file.

    The pd.ExcelWriter() function requires an existing Excel file to which it can add new sheets. If the file does not exist, it will create a new one. It seems like in your case, the file specified in outPath might not be a valid Excel file.

    You can try to add a check to see if the file exists and if not, create a new Excel file. Here is an example:

    
    import os
    import pandas as pd
    
    def write(pureList, resultList, outPath, sheetName):
        d = {'words':pureList,'translated':resultList}
        df = pd.DataFrame(data=d)
        if os.path.exists(outPath):
            with pd.ExcelWriter(outPath, mode='a', engine='openpyxl') as writer:
                df.to_excel(writer, sheet_name=sheetName)
        else:
            with pd.ExcelWriter(outPath, engine='openpyxl') as writer:
                df.to_excel(writer, sheet_name=sheetName)
    
    

    This code checks if the file specified in outPath exists using the os.path.exists() function. If it exists, it will append the new sheet to the existing file. If it does not exist, it will create a new Excel file with the specified sheet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 3月2日
  • 已采纳回答 2月22日
  • 创建了问题 2月20日

悬赏问题

  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥40 宿舍管理系统设计(c#)
  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?