表格文件(cvs、excel、ods、prn)的有效读取

cutplace的Python项目详细描述


Build StatusTest coverageCode Health

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可以用一种易于阅读的方式描述这样的文件。它包括 三部分。首先,有通用数据格式:

PropertyValue
DFormatDelimited
DEncodingUTF-8
DHeader1
DLine delimiterLF
DItem delimiter,

接下来是数据文件中存储的字段:

NameExampleEmptyLengthTypeRule
Fcustomer_id3798Integer0…99999
FsurnameMiller…60
Ffirst_nameJohnX…60
Fdate_of_birth1978-11-27DateTimeYYYY-MM-DD
FgendermaleXChoicefemale, male

也可以描述整个文件中必须满足的条件:

DescriptionTypeRule
Ccustomer must be uniqueIsUniquecustomer_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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java解析一个文本文件,如果内容是一种特定的格式,就打印它   java如何使jbutton在另一个组件中执行键盘笔划   连接到独立帐户的java Stripe   java在数据库连接过程中出错。。。初始连接   一个系列JavaFX中特定节点的java集样式   java重建recycler视图会导致它消失   在codechef上提交java Getting runtime(NZEC)时出错   Java类路径设置错误   java如何显示数组中2个数字之间的信息?   java为什么新的GC只有很长的用户时间?   如何解决java。关于此代码的lang.OutOfMemoryError   通过应用程序在Facebook粉丝页面上发布java帖子   java Red5服务器无法启动   系统输出Java读取文本文件外置行   java整数。Parse对excel获取的字符串抛出NumberFormatException   有一个Java数组列表;如何按每个数组的最后一个元素排序?   java需要txt文件中的数据(以逗号分隔)来用现有类中的对象填充arraylist