打造 CSV 框架:从基础到实践
1. 支持 CSV 方言选项
Python 的 csv
模块提供了丰富的选项以支持不同的方言。为了让我们的框架与之兼容,我们将原本的 Options
类重命名为 Dialect
,这样能更好地契合已有的术语。
为了避免手动列出 csv
支持的所有选项,我们采用了一种前瞻性的方法:直接将额外的选项传递给 csv
模块。借助 Python 的双星号语法,我们可以接收额外的关键字参数,并将它们存储在字典中,后续传递给 csv
函数。
以下是 Dialect
类的实现:
class Dialect:
"""
A container for dialect options that control how a CSV file should be
handled when converting it to a set of objects.
has_header_row
A Boolean indicating whether the file has a row containing header
values. If True, that row will be skipped when looking for data.
Defaults to False.
For a l