Python:解析类似JSON的Javascript数据结构(带连续逗号) [英] Python: parsing JSON-like Javascript data structures (w/ consecutive commas)

查看:172
本文介绍了Python:解析类似JSON的Javascript数据结构(带连续逗号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析类似JSON的字符串。它们与普通JSON的唯一区别在于数组中存在连续逗号。如果有两个这样的逗号,则隐含意味着应插入 null 。示例:

I would like to parse JSON-like strings. Their lone difference with normal JSON is the presence of contiguous commas in arrays. When there are two such commas, it implicitly means that null should be inserted in-between. Example:

       JSON-like:  ["foo",,,"bar",[1,,3,4]]
      Javascript:  ["foo",null,null,"bar",[1,null,3,4]]
Decoded (Python):  ["foo", None, None, "bar", [1, None, 3, 4]]

原生 json。 JSONDecoder 类不允许我改变数组解析的行为。我只能修改对象(dicts),整数,浮点数,字符串的解析器(通过将kwargs函数赋予 JSONDecoder(),请参阅文档)。

The native json.JSONDecoder class doesn't allow me to change the behavior of the array parsing. I can only modify the parser for objects (dicts), ints, floats, strings (by giving kwargs functions to JSONDecoder(), please see the doc).

那么,是吗?我是不是要从头开始编写JSON解析器?可以使用 json 的Python代码,但它非常混乱。我更喜欢使用它的内部而不是复制它的代码!

So, does it mean I have to write a JSON parser from scratch? The Python code of json is available but it's quite a mess. I would prefer to use its internals instead of duplicating its code!

推荐答案

因为你要解析的不是JSON 本身,而是一种非常类似于JSON的不同语言,您可能需要自己的解析器。

Since what you're trying to parse isn't JSON per se, but rather a different language that's very much like JSON, you may need your own parser.

幸运的是,这不是听起来很难听。您可以使用Python解析器生成器,如 pyparsing 。可以使用相当简单的无上下文语法完全指定JSON(我找到了一个这里),所以你应该能够修改它以满足你的需要。

Fortunately, this isn't as hard as it sounds. You can use a Python parser generator like pyparsing. JSON can be fully specified with a fairly simple context-free grammar (I found one here), so you should be able to modify it to fit your needs.

这篇关于Python:解析类似JSON的Javascript数据结构(带连续逗号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆