Fix unquoted keys in JSON-like file so that it uses correct JSON syntax

前端 未结 3 1592
野趣味
野趣味 2021-01-17 02:41

I have a very large JSON-like file, but it is not using proper JSON syntax: the object keys are not quoted. I\'d like to write a script to fix the file, so that I can load

3条回答
  •  一个人的身影
    2021-01-17 03:14

    I suggest matching whole words that are not enclosed into double quotation marks and adding quotation marks around them:

    import re
    p = re.compile(r'(?"', test_str)
    

    See IDEONE demo, output:

    {"abc" : "xyz", "cde" : {}, "fgh" : ["hfz"]}
    

提交回复
热议问题