用于处理csv或管道分隔数据等平面文件的可变记录类型

vinyl的Python项目详细描述


用于处理平面文件(如CSV或管道分隔数据)的可变记录类型。

处理扁平文件很痛苦。相反,使用乙烯基来声明性地描述和验证 通用平面文件格式。乙烯基为您提供方便的属性样式访问 类似于namedtuple,但使用适合轻松转换数据的可变数据类型。

importosimportcsvfromvinyl.recordimportRecordfromvinyl.fieldsimportVarCharField,FixedCharField# declare the data formatclassTestRecord(Record):customer_name=VarCharField(max_length=50)customer_number=FixedCharField(field_length=10,pad_with='0',justify='right')start_time=FixedCharField(value='00:00:00',field_length=8)# create an instancerecord=TestRecord()# read data from a filewithopen(os.path.join(os.path.dirname(__file__),'example.txt'))asf:reader=csv.reader(f)# copy a row of CSV data to the recordinstancerecord._load(*reader.next())# check the name was read correctlyassertrecord.customer_name=='a customer name with max length=50'# set a different customer numberrecord.customer_number='3'# number is reformatted according to the field settingsassertrecord.customer_number=='0000000003'# a static fieldassertrecord.start_time=='00:00:00'printrepr(record)# >>> TestRecord(customer_name=u'a customer name with max length=50',#                customer_number=u'0000000003', start_time=u'00:00:00')

支架

对于问题和源代码管理,请使用github:

https://github.com/mrj0/vinyl/

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

推荐PyPI第三方库


热门话题
java对来自同一基类的不同对象进行迭代   java打印在JtextArea或JtextPane上   java无法使用Play Core库提供的inapp更新支持更新应用程序   javafxgui中关注点的java分离   onPreferenceChange方法中的java奇怪值   lambda调用包含在Java中可选值的列表中?   servlet中的java int值(无法将字符串转换为int)   尽管有终止表达式,java循环仍不会结束   java是否可以通过编程方式启用远程jmx监视?   java如何创建集合的新实例   如何将自定义Java模块更新为JRE映像   java无法点击Chrome和IE11上的自定义复选框   java Here Android SDK在“无车无车”和“无车无车”街道上导航