Traceback (most recent call last):
File "create_and_update_xlsx_sheets.py", line 183, in <module>
create_and_update_worksheets()
File "create_and_update_xlsx_sheets.py", line 178, in create_and_update_worksheets
resize_columns(sheet)
File "/home/Dave/.virtualenvs/NHL/lib/python3.5/site-packages/pandas/io/excel.py", line 786, in __exit__
self.close()
File "/home/Dave/.virtualenvs/NHL/lib/python3.5/site-packages/pandas/io/excel.py", line 790, in close
return self.save()
File "/home/Dave/.virtualenvs/NHL/lib/python3.5/site-packages/pandas/io/excel.py", line 824, in save
return self.book.save(self.path)
File "/home/Dave/.virtualenvs/NHL/lib/python3.5/site-packages/openpyxl/workbook/workbook.py", line 345, in save
save_workbook(self, filename)
File "/home/Dave/.virtualenvs/NHL/lib/python3.5/site-packages/openpyxl/writer/excel.py", line 266, in save_workbook
writer.save(filename)
File "/home/Dave/.virtualenvs/NHL/lib/python3.5/site-packages/openpyxl/writer/excel.py", line 248, in save
self.write_data()
File "/home/Dave/.virtualenvs/NHL/lib/python3.5/site-packages/openpyxl/writer/excel.py", line 93, in write_data
archive.writestr(ARC_WORKBOOK, write_workbook(self.workbook))
File "/home/Dave/.virtualenvs/NHL/lib/python3.5/site-packages/openpyxl/writer/workbook.py", line 91, in write_workbook
active = get_active_sheet(wb)
File "/home/Dave/.virtualenvs/NHL/lib/python3.5/site-packages/openpyxl/writer/workbook.py", line 61, in get_active_sheet
raise IndexError("At least one sheet must be visible")
IndexError: At least one sheet must be visible
我熟悉那种错误。我从某个网站提取CSV数据,但订阅已过期,因此我收到了该错误。我想记录一个特定的错误,告诉我&#39;必须至少有一张纸可见。确认您对blabla.com的订阅仍处于有效状态。&#39;每当我有这种错误。我怎么能在python中做这样的事情
答案 0 :(得分:1)
使用try
和except
。
try:
# code that produces the error. ex:
active = get_active_sheet(wb) # or
raise IndexError("At least one sheet should be visible")
except IndexError as err:
logger.error("An error occurred whilst pulling CSV from %s. Please ensure your connection to %s is still active.", website)
如果您不知道记录器的工作原理,请查看this tutorial by Fang。如果你想知道更多关于Python中的异常,请查看评论中jonrsharpe给出的tutorial。