JSON和JSONish数据的解析器和pretyprinter
canif的Python项目详细描述
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
表达式:
或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
有控制输出的选项,例如缩进深度、尾随逗号等
- 项目
标签: