Python: parsing JSON-like Javascript data structures (w/ consecutive commas)

前端 未结 6 1815
南旧
南旧 2020-12-10 22:55

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 i

6条回答
  •  死守一世寂寞
    2020-12-10 23:12

    You can do the comma replacement of Lattyware's/przemo_li's answers in one pass by using a lookbehind expression, i.e. "replace all commas that are preceded by just a comma":

    >>> s = '["foo",,,"bar",[1,,3,4]]'
    
    >>> re.sub(r'(?<=,)\s*,', ' null,', s)
    '["foo", null, null,"bar",[1, null,3,4]]'
    

    Note that this will work for small things where you can assume there aren't consecutive commas in string literals, for example. In general, regular expressions aren't enough to handle this problem, and Taymon's approach of using a real parser is the only fully correct solution.

提交回复
热议问题