Python 如何用其他东西取代所有的存在价值

Python 如何用其他东西取代所有的存在价值,python,json,replace,jq,Python,Json,Replace,Jq,我想找到一种简单的方法来替换JSON文件中的值 我正在寻找类似于: json_file.replace("-", "") 样本: "scores": { "count": 1, "my-followings": 420, "my-plain-tweets": 0, "my-tweets-with-links": 1, "my-tweet

我想找到一种简单的方法来替换JSON文件中的值

我正在寻找类似于:

json_file.replace("-", "")
样本:

"scores": {
                "count": 1,
                "my-followings": 420,
                "my-plain-tweets": 0,
                "my-tweets-with-links": 1,
                "my-tweets-with-image": 0,
                "my-replies": 0,
                "my-listed": 113,
                "my-retweets": 0,
                "my-statuses": 3653,
                "my-followers": 8536,
                "favourites": 0,
                "my-tweets-with-video": 0,
                "my-favourites": 7929,
                "retweets": 0
            }
预期:

"scores": {
                "count": 1,
                "myfollowings": 420,
                "myplaintweets": 0,
                "mytweetswithlinks": 1,
                "mytweetswithimage": 0,
                "myreplies": 0,
                "mylisted": 113,
                "myretweets": 0,
                "mystatuses": 3653,
                "myfollowers": 8536,
                "favourites": 0,
                "mytweetswithvideo": 0,
                "myfavourites": 7929,
                "retweets": 0
            }

添加了示例和我期望的内容。

使用
str.replace

演示:

import json
with open(filename, "r") as infile:                    #Read json
    data = json.load(infile)

data = dict((k.replace("-", ""), v) for k, v in data["scores"].items())  #Remove "-"
with open(filename, "w") as outfile:                    #Write back to file
    data = json.dump({"scores": data}, outfile)
按注释编辑

命令行工具是为这类问题设计的

例如,假设要更改所有对象中所有键名(但仅键名)中的所有连字符, 无论嵌套有多深:

walk(if type == "object" then with_entries(.key |= gsub("-";"")) else . end)
如果您只想更改第一个hypen,您应该将
gsub
更改为
sub

如果您的jq没有
walk/1
,您可以包括其定义,例如

# Apply f to composite entities recursively, and to atoms
def walk(f):
  . as $in
  | if type == "object" then
      reduce keys_unsorted[] as $key
        ( {}; . + { ($key):  ($in[$key] | walk(f)) } ) | f
  elif type == "array" then map( walk(f) ) | f
  else f
  end;

walk(if type == "object" then with_entries(.key |= gsub("-";"")) else . end)
使用
海绵调用
如果您想覆盖输入文件,那么像
海绵
这样的实用程序就很方便了。例如,如果JSON在一个文件中,比如input.JSON,并且如果上面的jq程序在program.jq中,那么您可以按如下方式继续 如下:


为什么你发布的代码对你不好?看起来是删除所有dashesCan的好方法您可以发布JSON示例和预期输出?@Rakesh更新,看看……@AntiMatterDynamite我知道应该这样做,我确信这是一个愚蠢的错误,只是不知道,什么。@DenisSineiko但它做了什么?输出或错误消息是什么?这是一个很好的示例,谢谢。。。但是,有没有可能在整个JSON文件中替换。。。(就像你一样,只需一个音符++点击CTRH+H并用其他符号替换所有现有符号)更新的代码段。可以使用需要替换的符号链接替换函数。例如:
.replace(“-”,”).replace(“*”,”).replace(“?”,”)
当我读取和替换时,一切都很好(它确实工作,我可以读取文件并替换),但当我将其写回文件时(文件为空),没有错误。将数据写入同一个文件的问题(就像我写入不同的文件一样,它确实有效。您使用的代码是否与上述示例中发布的代码完全相同?谢谢,我已将其更改为w+,并且它开始正常工作)
# Apply f to composite entities recursively, and to atoms
def walk(f):
  . as $in
  | if type == "object" then
      reduce keys_unsorted[] as $key
        ( {}; . + { ($key):  ($in[$key] | walk(f)) } ) | f
  elif type == "array" then map( walk(f) ) | f
  else f
  end;

walk(if type == "object" then with_entries(.key |= gsub("-";"")) else . end)
jq -f program.jq input.json | sponge input.json