固定列数据文件的快速解析器
lineparser的Python项目详细描述
行分析器
lineparser是一个小型库,其目标是:非常快速地解析固定宽度格式的文件。 为了实现这一点,lineparser使用cython获得good old提供的速度 c代码和python的便利性。
文档可以找到here。
安装
如果您在Windows或Linux 64位计算机上,则应该能够通过PIP安装lineparser。
$ pip3 install lineparser
示例
demo.py:
importlineparserimporttimefields=[(lineparser.Float64,12),(lineparser.Float64,10),(lineparser.Float64,12),(lineparser.String,6),(lineparser.String,6),(lineparser.String,14),(lineparser.String,14),(lineparser.Float64,6)]try:start=time.time()# On a successfull parse, result will be a list of lists and numpy arrays (strings will be in # lists, and numbers will be in numpy arrays). result=lineparser.parse(fields,b'data/small_data.par')end=time.time()print(f"Took {end - start} seconds to parse")exceptlineparser.LineParsingErrorase:print(f"Encountered the following error while trying to parse:\n {str(e)}")
data/small_data.par
31.43339 6.531E-28 31.442390 3 2 0.00048 0.00000 100
41.89467 1.415E-26 62.878170 4 3 0.00065 0.00000 100
41.89786 3.538E-27 62.876840 4 3 0.00064 0.00000 100
...
运行示例:python3 demo.py
从源安装
从源代码安装也很容易。必须在计算机上安装gcc,并且必须 cython仅当您要修改库时才安装。然后运行此命令:
$ python3 setup.py install
建筑
lineparser构建起来很简单,应该只需要一个命令:
$ python3 setup.py build_ext --inplace
然后您应该能够将其导入到python解释器中(在构建目录中):
$ python3 setup.py build_ext --inplace
$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lineparser
>>> ...