pd.read_excel / ExcelWriter 引发 BadZipFile:文件不是 zip 文件

问题描述 投票:0回答:1

我正在尝试将一系列 Excel 文件与 Pandas 连接起来。我创建了一个 for 循环来读取所提供目录的所有文件,并在我想要读取的列旁边指定了工作表名称。读取这些文件后,我将它们连接为一个 DataFrame 变量,我将其命名为“list_of_df”。
我已经测试了我的代码,直到它们被连接起来,一旦我开始连接各种 Excel 工作表,我就可以看到错误被初始化。目前我收到错误“文件不是 zip 文件”

import pandas as pd
import glob

df = []
list_of_df = pd.DataFrame()

for filename in all_files:
    df = pd.read_excel(filename, 
                       sheet_name = 'cycle', 
                       usecols=['Chg. Cap.(mAh)','DChg. Cap.(mAh)'], 
                       engine = 'openpyxl')
    list_of_df = pd.concat([list_of_df. df], ignore_index = True)

print(list_of_df)

with pd.ExcelWriter('output3.xlsx',  engine = 'openpyxl', mode = 'a',) as writer:
    df.to_excel(writer, sheet_name = 'output3')

这是引发的错误:

Traceback (most recent call last):

  File ~\AppData\Local\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\documents\python scripts\chg._and_dchg._curves_for_excel5.py:30
    df = pd.read_excel(filename, sheet_name = 'cycle', usecols=['Chg. Cap.(mAh)','DChg. Cap.(mAh)'], engine = 'openpyxl')

  File ~\AppData\Local\anaconda3\Lib\site-packages\pandas\util\_decorators.py:211 in wrapper
    return func(*args, **kwargs)

  File ~\AppData\Local\anaconda3\Lib\site-packages\pandas\util\_decorators.py:331 in wrapper
    return func(*args, **kwargs)

  File ~\AppData\Local\anaconda3\Lib\site-packages\pandas\io\excel\_base.py:482 in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)

  File ~\AppData\Local\anaconda3\Lib\site-packages\pandas\io\excel\_base.py:1695 in __init__
    self._reader = self._engines[engine](self._io, storage_options=storage_options)

  File ~\AppData\Local\anaconda3\Lib\site-packages\pandas\io\excel\_openpyxl.py:557 in __init__
    super().__init__(filepath_or_buffer, storage_options=storage_options)

  File ~\AppData\Local\anaconda3\Lib\site-packages\pandas\io\excel\_base.py:545 in __init__
    self.book = self.load_workbook(self.handles.handle)

  File ~\AppData\Local\anaconda3\Lib\site-packages\pandas\io\excel\_openpyxl.py:568 in load_workbook
    return load_workbook(

  File ~\AppData\Local\anaconda3\Lib\site-packages\openpyxl\reader\excel.py:315 in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,

  File ~\AppData\Local\anaconda3\Lib\site-packages\openpyxl\reader\excel.py:124 in __init__
    self.archive = _validate_archive(fn)

  File ~\AppData\Local\anaconda3\Lib\site-packages\openpyxl\reader\excel.py:96 in _validate_archive
    archive = ZipFile(filename, 'r')

  File ~\AppData\Local\anaconda3\Lib\zipfile.py:1302 in __init__
    self._RealGetContents()

  File ~\AppData\Local\anaconda3\Lib\zipfile.py:1369 in _RealGetContents
    raise BadZipFile("File is not a zip file")

BadZipFile: File is not a zip file
python pandas excel concatenation xlsxwriter
1个回答
0
投票

总结评论的价值:

  1. 错误回溯表明问题出在 Excel 文件df = pd.read_excel(filename, sheet_name = 'cycle', usecols=['Chg. Cap.(mAh)','DChg. Cap.(mAh)'], engine = 'openpyx)
    读取
    ,而不是连接后的写入,正如您似乎相信的那样。
  2. 报告了两种可能的原因:
文件损坏可能太容易成为罪魁祸首,但没有给出任何证据。另一方面,敏感度标签看起来有据可查。

基于此,将这篇文章变成一篇有用的文章,对您和现在/未来遇到同样问题的人来说,

    本着 MRE 的精神,将您的帖子重新表述为阅读导致/不导致问题的一两个文件(最好由您自己完成重大编辑 - 如果我这样做,可能不会被接受)
  • 请报告您的解决方案或进一步的困难
© www.soinside.com 2019 - 2024. All rights reserved.