pandas 尝试将2个excel合并为一个时出现错误消息

xurqigkl  于 2023-06-20  发布在  其他
关注(0)|答案(1)|浏览(151)

下面的代码应该将2个excels合并为一个,但由于某种原因导致了错误:

import pandas as pd

JL_MP_PE_07A = pd.read_excel(r'D:\Maintenance\Bloc 6\PROJECT KPI\Performance\JL-MP-PE-07A.xlsx', skiprows=22, engine='openpyxl')
JL_MP_PE_07B = pd.read_excel(r'D:\Maintenance\Bloc 6\PROJECT KPI\Performance\JL-MP-PE-07B.xlsx', skiprows=23, engine= 'openpyxl')

merged_df = pd.concat([JL_MP_PE_07A, JL_MP_PE_07B])

merged_df.to_excel('D:\Maintenance\Bloc 6\PROJECT KPI\Performance\merged_OT_file.xlsx', index=False)

我运行了这个程序,我得到了以下错误,我尝试ziping每个文件,我在每个路径的开始添加了'r'。还是个问题

File "d:\Maintenance\Bloc 6\PROJECT KPI\Performance\Source code\Performance_Excel merger.py", line 5, in <module>
    JL_MP_PE_07B = pd.read_excel(r'D:\Maintenance\Bloc 6\PROJECT KPI\Performance\JL-MP-PE-07B.xlsx', skiprows=23, engine= 'openpyxl')    
   
  File "C:\Users\seljoukia\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_base.py", line 478, in read_excel  
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\seljoukia\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_base.py", line 1513, in __init__   
    self._reader = self._engines[engine](self._io, storage_options=storage_options)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\seljoukia\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_openpyxl.py", line 549, in __init__
    super().__init__(filepath_or_buffer, storage_options=storage_options)
  File "C:\Users\seljoukia\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_base.py", line 540, in __init__
    self.book = self.load_workbook(self.handles.handle)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\seljoukia\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\io\excel\_openpyxl.py", line 560, in load_workbook
    return load_workbook(
           ^^^^^^^^^^^^^^
  File "C:\Users\seljoukia\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\reader\excel.py", line 344, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\seljoukia\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\reader\excel.py", line 123, in __init__
    self.archive = _validate_archive(fn)
                   ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\seljoukia\AppData\Local\Programs\Python\Python311\Lib\site-packages\openpyxl\reader\excel.py", line 95, in _validate_archive
    archive = ZipFile(filename, 'r')
              ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\seljoukia\AppData\Local\Programs\Python\Python311\Lib\zipfile.py", line 1301, in __init__
    self._RealGetContents()
  File "C:\Users\seljoukia\AppData\Local\Programs\Python\Python311\Lib\zipfile.py", line 1368, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
mf98qq94

mf98qq941#

请确保避免引擎或更改为xlrd它可能会解决您的错误

相关问题