开始使用免费开始使用

在加载数据时处理异常

有时,您的数据管道会抛出异常。异常是一种告警形式,它会在出现意外情况时提醒数据工程师。正确处理这些异常非常重要。本练习将带您进行实践!

为方便开始,已将 pandaspd 的名称导入,同时导入了 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)
编辑并运行代码