表格文件(cvs、excel、ods、prn)的有效读取
cutplace的Python项目详细描述
CutPlace是一个工具和API,用于验证存储在CSV中的表格数据, excel、ods和prn文件符合cutplace接口定义(cid)。
例如,考虑以下存储数据的customers.csv文件 关于客户:
customer_id,surname,first_name,born,gender 1,Beck,Tyler,1995-11-15,male 2,Gibson,Martin,1969-08-18,male 3,Hopkins,Chester,1982-12-19,male 4,Lopez,Tyler,1930-10-13,male 5,James,Ana,1943-08-10,female 6,Martin,Jon,1932-09-27,male 7,Knight,Carolyn,1977-05-25,female 8,Rose,Tammy,2004-01-12,female 9,Gutierrez,Reginald,2010-05-18,male 10,Phillips,Pauline,1960-11-09,female
cid可以用一种易于阅读的方式描述这样的文件。它包括 三部分。首先,有通用数据格式:
Property | Value | |
---|---|---|
D | Format | Delimited |
D | Encoding | UTF-8 |
D | Header | 1 |
D | Line delimiter | LF |
D | Item delimiter | , |
接下来是数据文件中存储的字段:
Name | Example | Empty | Length | Type | Rule | |
---|---|---|---|---|---|---|
F | customer_id | 3798 | Integer | 0…99999 | ||
F | surname | Miller | …60 | |||
F | first_name | John | X | …60 | ||
F | date_of_birth | 1978-11-27 | DateTime | YYYY-MM-DD | ||
F | gender | male | X | Choice | female, male |
也可以描述整个文件中必须满足的条件:
Description | Type | Rule | |
---|---|---|---|
C | customer must be unique | IsUnique | customer_id |
cid可以以常用的电子表格格式存储,特别是 excel和ods,例如cid_customers.ods。
cutplace可以验证数据文件是否符合cid:
$ cutplace cid_customers.ods customers.csv
现在添加一个新的带有断开的date_of_birth:
73921,Harris,Diana,04.08.1953,female
CutPlace拒绝此文件,并显示错误消息:
customers.csv (R12C4): cannot accept field ‘date_of_birth’: date must match format YYYY-MM-DD (%Y-%m-%d) but is: ‘04.08.1953’
此外,cutplace还提供了一个易于使用的读写api 使用公共接口的表格数据文件,无需处理 数据格式特定模块的内在特性。阅读并验证 以上示例:
import cutplace import cutplace.errors cid_path = 'cid_customers.ods' data_path = 'customers.csv' try: for row in cutplace.rows(cid_path, data_path): pass # We could also do something useful with the data in ``row`` here. except cutplace.errors.DataError as error: print(error)
有关详细信息,请阅读 http://cutplace.readthedocs.org/或访问项目 https://github.com/roskakori/cutplace。