JSON和JSONish数据的解析器和pretyprinter

canif的Python项目详细描述


Build StatusPyPI version

JSON和JSON-ish数据的解析器和漂亮的打印机,在命令行上或作为Python库。在

概要

假设您有一个JavaScript对象文本:

$ cat input.js
setConfig({editable: true, latlng: new LatLng(48.1434, 17.1082), last_modified: new Date(1995, 11, 17)})

或Python repr表达式:

^{pr2}$

或MongoDB文档:

$ cat input.mongo
{"id": ObjectId("507f191e810c19729de860ea"), "last_modified": ISODate("2020-05-20T00:00:00Z")}

您可以使用canif库将该数据作为普通旧数据结构加载:

>>>withopen('input.js','rt',encoding='UTF-8')asfile_in:...canif.load(file_in){'$$setConfig':[{'editable':True,'latlng':{'$$new LatLng':[48.1434,17.1082]},'last_modified':{'$$new Date':[1995,11,17]}}]}
>>>withopen('input.py','rt',encoding='UTF-8')asfile_in:...canif.load(file_in){'_id':73,'occur':{'$$OrderedDict':[[('doc1',10),('doc2',1)]]},'match':'$repr<_sre.SRE_Match object at 0x7f2b9eaa5f80>'}
>>>withopen('input.mongo','rt',encoding='UTF-8')asfile_in:...canif.load(file_in){'id':{'$oid':['507f191e810c19729de860ea']},'last_modified':{'$$ISODate':['2020-05-20T00:00:00Z']}}

可以把它看作是json.loads的一个版本,它接受更广泛的语法。在

这在编写Web scraper时非常有用,因为它允许您解析JavaScript数据结构,而不必使用 JavaScript解释器或完整的语法分析器。在

命令行用法

您还可以使用canif命令行工具漂亮地打印JSON-ish数据:

$ canif < input.js
setConfig(    {        editable: true,        latlng: new LatLng(            48.1434,            17.1082,        ),        last_modified: new Date(            1995,            11,            17,        ),    },)
$ canif < input.py
{    "_id": 73,    "occur": OrderedDict(        [            (                "doc1",                10,            ),            (                "doc2",                1,            ),        ],    ),    "match": <_sre.SRE_Match object at 0x7f2b9eaa5f80>,}
$ canif < input.mongo
{    "id": ObjectId(        "507f191e810c19729de860ea",    ),    "last_modified": ISODate(        "2020-05-20T00:00:00Z",    ),}

可以选择将数据转换为适当的JSON:

$ canif -j < input.js
{    "$$setConfig": [        {            "editable": true,            "latlng": {                "$$new LatLng": [                    48.1434,                    17.1082                ]            },            "last_modified": {                "$$new Date": [                    1995,                    11,                    17                ]            }        }    ]}

程序在加载数据时会很好地打印数据,因此它应该能够处理非常大的文件。在

使用--help快速浏览可用选项:

$ canif --help
usage: canif [-h] [-i N | -f] [-j] [-T] [--single-document]             [--ensure-ascii] [-I ENCODING] [-O ENCODING]Pretty-print JSON and JSON-ish dataoptional arguments:  -h, --help            show this help message and exit  -i N, --indent N      Indent each level by N spaces (0 means flat,                        single-line output)  -f, --flatten         Flatten output (equivalent to -i 0)  -j, --json-output     Convert data to valid JSON if it wasn't                        already (e.g. None becomes null)  -T, --no-trailing-commas                        Don't insert trailing commas after the last                        item in a sequence. This is implied by                        --json-output.  --single-document     Check that the input consists of a single                        document, rather that the default of                        accepting a stream of documents  --ensure-ascii        Ensure JSON output is ASCII by using \uXXXX                        sequences in place of non-ASCII characters  -I ENCODING, --input-encoding ENCODING                        Character set used for decoding the input                        (default: UTF-8)  -O ENCODING, --output-encoding ENCODING                        Character set used for encoding the output                        (default: UTF-8)

Emacs绑定

如果您碰巧使用了Emacs的shell-mode,这对于格式化任何Python程序的日志或输出都非常有用。在

请参阅canif.el获取一个简单的函数,该函数可以绑定到击键操作,并且可以很好地打印数据结构 在点上。在

支持的格式

Canif用于“JSON和JSON-ish”数据。它可以解析:

  • 纯JSON
  • JavaScript对象文本,包括:
    • 未加引号的键
    • 尾随逗号
    • //评论
    • 空数组插槽(例如[1,,,4]
  • 打印的Python数据结构,包括
    • 集合:{42}
    • 元组:("one", "two", "three")
    • <repr>表示法
  • 云数据库BSON

解析后的数据可以以两种格式打印出来:

  • 逐字(即只修改空格和逗号)
  • 严格的JSON

有控制输出的选项,例如缩进深度、尾随逗号等

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

推荐PyPI第三方库


热门话题
java如何让用户决定按钮的特定颜色   java Tomcat jdbc连接池,使用后不释放连接   Java泛型类可分配性   javaactivemq&如何在路由路径中创建依赖关系   java为什么没有接收到操作用户?   windows如何启动使用cmd中预编译DLL的java swing应用程序?   java JFreechart实时组合图,在未收到数据点的情况下呈现step子图表的上一个值   java排序自定义数组列表   java如何从HSLFSlideShow获取文本格式信息   java不能将片段和活动登录结合起来   java是下载位于远程存储服务中的文件的有效方法   java AS:将点数交给GameOverActivity   java如何在textView中将焦点放在新生成文本的顶部?   HashMap中特定于Java存储的类类型   java使用不同的变量类型进行计算   if语句中的Java poll()   检查匹配括号的java字符堆栈没有错误,但也没有任何作用   java Netbeans不断将应用程序部署到错误的服务器