載入資料時處理例外
在實務上,你的資料管線有時可能會拋出例外。這些例外是一種警示機制,能讓資料工程師知道發生了非預期事件。妥善處理這些例外非常重要。在本練習中,你就要來練習這件事!
為了幫助你開始,已經將 pandas 以 pd 匯入,並匯入了 logging 模組。預設日誌層級已設為 "debug"。
本練習屬於課程
使用 Python 的 ETL 與 ELT
練習說明
- 更新管線以加入
try區塊,並嘗試從路徑"sales_data.parquet"讀取資料。 - 若無法將檔案讀入
pandas的 DataFrame,捕捉FileNotFoundError。 - 建立一則錯誤層級的日誌來記錄此次失敗。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def extract(file_path):
return pd.read_parquet(file_path)
# Update the pipeline to include a try block
____:
# Attempt to read in the file
raw_sales_data = extract("____")
# Catch the FileNotFoundError
except ____ as file_not_found:
# Write an error-level log
logging.____(file_not_found)