根据任务要求审查这个生成的读取器,找出四类不同风险。
从 data_root 下不可信的文件名惰性读取正数订单金额,跳过可选表头,并在错误或提前结束时释放文件。
Python
from pathlib import Path
def iter_orders(data_root, filename):
path = Path(data_root) / filename
source = path.open(encoding="utf-8")
rows = (line.rstrip().split(",") for line in source)
next(rows)
cache = []
for fields in rows:
order_id, total_text = fields
total_cents = int(total_text)
if total_cents:
cache.append((order_id, total_cents))
source.close()
for order in cache:
yield order
生成代码仅作示例,不代表任何特定模型