一分钟我的脚本连续多天工作,第二分钟我收到此错误。
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)
代码未编辑或其他任何操作,刚开始不起作用。
答案 0 :(得分:2)
正如其他人已经指出的,损坏的文件是罪魁祸首。
执行这些快速的健全性检查:
就我而言,我手动检查了 excel 文件内容,结果它是空的,因为我没有正确存储文件。一旦我解决了这个问题,“文件不是 zip 文件”错误就解决了。
答案 1 :(得分:1)
这是一个非常普遍的问题,许多人正在尝试解决。它与excel文件和openpyxl有关。就像@Barmar在他的评论中所说,xlsx,xlsm等确实是zip。直到python 2.7都运行良好。
尝试读取和写入csv,这不会有问题。
答案 2 :(得分:0)
Excel XLSL
文件已压缩,XLS
文件未压缩。
我相信此错误与以下情况的组合有关
XLS
未压缩,并且openpyxl
必须与XLSL
文件一起使用。通过检查上传哪种类型的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,则可以执行以下测试:
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")