构建 CSV 框架:从基础到实践
1. 选项容器与方言类
在处理 CSV 文件时,我们首先需要一个选项容器来管理各种配置。最初,我们有一个简单但实用的选项容器,它相较于普通字典的优势在于能自动禁止使用未指定的选项。考虑到 Python 的 csv 模块支持多种方言选项,并且我们的框架很多功能依赖于该模块,因此将选项类重命名为 Dialect 更合适,这样能更好地与已有词汇保持一致。
为了支持 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 li
超级会员免费看
订阅专栏 解锁全文
1715

被折叠的 条评论
为什么被折叠?



