一个Python包,简化了检索、组织和更改数据的过程。
fluxif的Python项目详细描述
流质
A Python package that eases the process of retrieving, organizing and altering data.
所需软件包
- 熊猫
- imperium
- ijson
安装
pip install fluxify
主要班级
fluxify.mapper.Mapper
这个类用于读取和处理带有少量数据的快速文件,这些数据可以加载到内存中。在
fluxify.lazy_mapper.LazyMapper
你可能已经猜到了,这个类用于迭代大型数据文件,不管它是CSV格式, JSON或XML。在
使用
从一个简单的CSV文件中检索数据
^{pr2}$映射器实现
fromfluxify.mapperimportMapperimportyaml# Could also be loaded from a fileyamlmapping="""brand: col: 1price: col: 2state: col: 3publish_date: col: 4 transformations: - { transformer: 'date', in_format: '%Y-%m-%d %H:%M:%S', out_format: '%H:%M %d/%m/%Y' }is_new: conditions: - condition: "subject['state'] == 'new'" returnOnSuccess: True returnOnFail: False"""Map=yaml.load(yamlmapping,Loader=yaml.FullLoader)mapper=Mapper(_type='csv')data=mapper.map('path/to/csvfile.csv',Map)print(data)
Output
[{'brand': 'Xaomi', 'price': '390.90', 'state': 'used', 'published_date': '12:32 03/01/2020''is_new': False }, {'brand': 'iPhone', 'price': '1299.90', 'state': 'new', 'published_date': '09:48 02/01/2020''is_new': True }]
LazyMapper实现
LazyMapper
在结尾处不返回所有映射的数据,
相反,它将数据映射成可以指定的小尺寸,这样就不会最大限度地占用内存。在
fromfluxify.lazy_mapperimportLazyMapperimportyaml# Could also be loaded from a fileyamlmapping="""brand: col: 1price: col: 2state: col: 3publish_date: col: 4 transformations: - { transformer: 'date', in_format: '%Y-%m-%d %H:%M:%S', out_format: '%H:%M %d/%m/%Y' }is_new: conditions: - condition: "subject['state'] == 'new'" returnOnSuccess: True returnOnFail: False"""Map=yaml.load(yamlmapping,Loader=yaml.FullLoader)mapper=LazyMapper(_type='csv',error_tolerance=True,bulksize=500)mapper.map('path/to/csvfile.csv',Map)defsome_callback(results):foriteminresults:pass# Perform some actionmapper.set_callback(some_callback)mapper.map('path/to/csvfile.csv',Map)
如您所见,在本例中,映射器每次累积500个映射项时都会调用回调函数。在
映射设置
col
键用于指定必须从中检索值的列号或属性名。
如果要将输入数据指定为检索值,请使用_all_
作为col
的值
transformations
键用于对检索到的值应用转换。可用变压器如下所示。在
conditions
键用于应用条件并更改检索到的值。
这些条件使用Python语法,但不能使用Python的所有本机函数。
可用功能如下所示。在
default
用于定义检索到的值为null时的默认值
Warning:如果使用值定义了default
键,则在应用转换之前将应用该键
和条件。在
JSON和XML的特殊情况
XML
如果要从多个同名XML标记中检索数据,请将multiple
设置为true
。
如果您只想从多个XML标记中检索一个值,请将index
键与multiple: true
一起使用。在
检索XML值时,默认行为是检索标记的.text
值。
如果要更改此设置,要检索包含许多其他标记的标记,请使用raw
键并将其设置为false
。
这将返回一个xml.etree.Element
类型的对象,您可以稍后对该对象应用转换以更改,
组织和检索数据。在
JSON
使用index
键从数组中检索特定值。
当然,只有当检索到的值是array类型时,它才有效。在
支持的格式
Format | CSV | JSON | XML | TXT |
---|---|---|---|---|
Supported | YES | YES | YES | NO |
变压器
Fluxify有内置的变压器,可以改变/修改数据。在
^{tb2}$例外情况
由于不同的原因,Fluxify有不同的异常类
它们位于exceptions子包fluxify.exceptions
Class | Arguments | Description |
---|---|---|
ArgumentNotFoundException | message | This exception is raised whenever a argument is not found. |
InvalidArgumentException | message | This exception is raised when a passed parameter/argument is invalid. |
ConditionNotFoundException | message | This exception is raised when the "condition" key is not defined in the mapping. |
UnsupportedTransformerException | message | This exception is raised when a transformer other than the ones defined above, is used. |
- 项目
标签: