小编Lew*_*een的帖子

如何从 json 文件中删除某些注释?(/*)

我有大约 500 个带有注释的 json 文件。尝试使用新值更新 json 文件上的字段会引发错误。我设法使用 commentjson 删除像这样的字符串 // some text 和 json 文件更新并且不抛出任何错误。

但大约有 100 个 json 文件,其注释如下:

  /*

   1. sometext.
        i. sometext
        ii. sometext 
   2. sometext

  */
Run Code Online (Sandbox Code Playgroud)

当 /* 存在时 Commentjson 就会崩溃。如果我删除 /* 并运行代码,它将起作用并更新并删除任何 //。如何编写一些代码来管理 /* 和 /* */ 之间的所有文本?

这是我当前的代码,可以删除//

with open(f"{i['Location']}\\{file_name}",'r') as f:
    json_info = commentjson.load(f) #Gets info from the json file
    json_info['password'] = password

    with open(f"{i['location_Daily']}\\{file_name}",'w') as f:
        commentjson.dump(json_info,f,indent = 4) #updates the password   
        print("updated")
Run Code Online (Sandbox Code Playgroud)

python json

1
推荐指数
1
解决办法
829
查看次数

标签 统计

json ×1

python ×1