gpt4 book ai didi

python - "BadZipFile: File is not a zip file"- 突然弹出错误

转载 作者:行者123 更新时间:2023-12-01 07:18:39 27 4
gpt4 key购买 nike

前一分钟我的脚本连续工作几天,下一分钟我收到此错误。

  File "<ipython-input-196-abdb28a77366>", line 1, in <module>
runfile('F:/-/-/-/cleaner_games_appstore_babil.py', wdir='F:/-/-/-')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "F:/-/-/-/cleaner_games_appstore_babil.py", line 112, in <module>
append_df_to_excel("stillfront.xlsx", dff, sheet_name='Apple_Babil', startrow=None, truncate_sheet=False, engine='openpyxl', header = False)

File "F:/-/-/-/cleaner_games_appstore_babil.py", line 84, in append_df_to_excel
writer.book = load_workbook(filename)

File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\reader\excel.py", line 311, in load_workbook
data_only, keep_links)

File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\reader\excel.py", line 126, in __init__
self.archive = _validate_archive(fn)

File "C:\ProgramData\Anaconda3\lib\site-packages\openpyxl\reader\excel.py", line 98, in _validate_archive
archive = ZipFile(filename, 'r')

File "C:\ProgramData\Anaconda3\lib\zipfile.py", line 1222, in __init__
self._RealGetContents()

File "C:\ProgramData\Anaconda3\lib\zipfile.py", line 1289, in _RealGetContents
raise BadZipFile("File is not a zip file")

BadZipFile: File is not a zip file

澄清一下,我不使用任何 zip 文件。我在 StackOverflow 上找到了代码,但没有提到代码不起作用或发生错误。

该脚本应该将我的 pandas DataFrame 写入 Excel 工作表。这是产生错误的代码部分:

def append_df_to_excel(filename, df, sheet_name='Apple_Babil', startrow=None,
truncate_sheet=False,
**to_excel_kwargs):

# ignore [engine] parameter if it was passed
if 'engine' in to_excel_kwargs:
to_excel_kwargs.pop('engine')

writer = pd.ExcelWriter(filename, engine='openpyxl')
try:

# try to open an existing workbook
writer.book = load_workbook(filename)
# get the last row in the existing Excel sheet
# if it was not specified explicitly
if startrow is None and sheet_name in writer.book.sheetnames:
startrow = writer.book[sheet_name].max_row

# truncate sheet
if truncate_sheet and sheet_name in writer.book.sheetnames:
# index of [sheet_name] sheet
idx = writer.book.sheetnames.index(sheet_name)
# remove [sheet_name]
writer.book.remove(writer.book.worksheets[idx])
# create an empty sheet [sheet_name] using old index
writer.book.create_sheet(sheet_name, idx)
# copy existing sheets
writer.sheets = {ws.title:ws for ws in writer.book.worksheets}

except FileNotFoundError:
# file does not exist yet, we will create it
pass

if startrow is None:
startrow = 0
# write out the new sheet
df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)
# save the workbook
writer.save()

append_df_to_excel("stillfront.xlsx", dff, sheet_name='Apple_Babil', startrow=None, truncate_sheet=False, engine='openpyxl', header = False)

代码没有被编辑或任何东西,只是开始不起作用。

最佳答案

Excel XLSX 文件已压缩,XLS 文件则未压缩。

我相信这个错误与以下组合有关

  1. XLS 未压缩,并且
  2. 自 python-3.9 起,openpyxl模块必须与 XLSX 文件一起使用。

通过检查上传的 Excel 文件类型并使用适当的引擎读入 Pandas,这个问题很容易解决。

按文件扩展名

from pathlib import Path
import pandas as pd

file_path = Path(filename)
file_extension = file_path.suffix.lower()[1:]

if file_extension == 'xlsx':
df = pd.read_excel(file.read(), engine='openpyxl')
elif file_extension == 'xls':
df = pd.read_excel(file.read())
elif file_extension == 'csv':
df = pd.read_csv(file.read())
else:
raise Exception("File not supported")

按文件 mimetype

如果您碰巧有权访问文件 mimetype,则可以执行以下测试:

import pandas as pd

if file.content_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
df = pd.read_excel(file.read(), engine='openpyxl') # XLSX
elif file.content_type == 'application/vnd.ms-excel':
df = pd.read_excel(file.read()) # XLS
elif file.content_type == 'text/csv':
df = pd.read_csv(file.read()) # CSV
else:
raise Exception("File not supported")

关于python - "BadZipFile: File is not a zip file"- 突然弹出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57817758/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com