跳转到主要内容

一个声明性对象转换器和格式化工具,用于聚合嵌套数据。

项目描述

glom

以Python方式重构数据

真实的应用程序有真实的数据,真实的数据是嵌套的。对象嵌套在对象中,对象嵌套在列表中。

glom是一种处理真实世界数据的新方法,它具有以下特点:

  • 基于路径访问嵌套数据结构
  • 可读性强的错误消息
  • 声明性数据转换,使用轻量级、Pythonic规范
  • 内置数据探索和调试功能

所有这些以及更多,作为完全文档化的、纯Python包,在Python 3.7+上进行了测试,以及PyPy3。安装就像

  pip install glom

当您安装glom时,您还可以获得glom命令行界面,让您可以在控制台中实验,但从不限制您使用shell脚本

Usage: glom [FLAGS] [spec [target]]

Command-line interface to the glom library, providing nested data access and data
restructuring with the power of Python.

Flags:

  --help / -h                     show this help message and exit
  --target-file TARGET_FILE       path to target data source (optional)
  --target-format TARGET_FORMAT
                                  format of the source data (json, python, toml,
                                  or yaml) (defaults to 'json')
  --spec-file SPEC_FILE           path to glom spec definition (optional)
  --spec-format SPEC_FORMAT       format of the glom spec definition (json, python,
                                    python-full) (defaults to 'python')
  --indent INDENT                 number of spaces to indent the result, 0 to disable
                                    pretty-printing (defaults to 2)
  --debug                         interactively debug any errors that come up
  --inspect                       interactively explore the data

您可以在命令行上完成任何事情都可以轻松转换为Python代码,因此当复杂性开始增加时,您始终有一个前进的方向。

示例

没有glom

>>> data = {'a': {'b': {'c': 'd'}}}
>>> data['a']['b']['c']
'd'
>>> data2 = {'a': {'b': None}}
>>> data2['a']['b']['c']
Traceback (most recent call last):
...
TypeError: 'NoneType' object is not subscriptable

有glom

>>> glom(data, 'a.b.c')
'd'
>>> glom(data2, 'a.b.c')
Traceback (most recent call last):
...
PathAccessError: could not access 'c', index 2 in path Path('a', 'b', 'c'), got error: ...

了解更多信息

如果所有这些都很有趣,请继续探索下面的glom

上面的所有链接都充满了示例,但如果你发现关于文档或glom本身有任何不足之处,请提交一个issue

与此同时,请记住:当你有嵌套数据时,glom它!☄️

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

glom-23.5.0.tar.gz (196.7 kB 查看哈希值)

上传时间

构建分布

glom-23.5.0-py3-none-any.whl (102.7 kB 查看哈希值)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面