[英]Replace values from YAML in JSON file - Python 3
I want to replace the values from YAML file into a JSON file using Python 3.我想使用 Python 3 将 YAML 文件中的值替换为 JSON 文件。
I have many JSON files where I want to get the values from a master YAML file and replace only certain values like source server ip, email, hostname.我有许多 JSON 文件,我想从主 YAML 文件中获取值并仅替换某些值,例如源服务器 ip、Z0C4EBAZF57C2ABname.C7BACZF57C2ABname.C7BCA39A
Eg I have this YAML file (mast_conf.yaml):例如,我有这个 YAML 文件(mast_conf.yaml):
- sourcesystem1:
sourceServer: 1.2.3.500
MailTo: gokul@gmail.com
- sourcesystem2:
sourceServer1: 2.2.3.500
sourceServer2: 3.2.3.500
MailTo: gokul@gmail.com
A JSON file (sourcesystem1.json): JSON 文件(sourcesystem1.json):
{
"source":"sourcesystem1",
"frequency":"daily",
"sourceServer":"1.2.1.2",
"hostName":"1.2.1.3",
"fileFormat":"csv",
"delimiterType":"semicolon"
}
Another JSON file (sourcesystem2.json):另一个 JSON 文件(sourcesystem2.json):
{
"source":"sourcesystem2",
"frequency":"daily",
"sourceServer":"1.2.3.2",
"hostName":"1.2.1.7",
"fileFormat":"csv",
"delimiterType":"commaseperated"
}
Below is my code I am trying out to parse the value from the json file下面是我试图解析 json 文件中的值的代码
import json
import yaml
with open("master_conf.yaml", 'r') as f:
yaml_config = yaml.safe_load(f)
yaml_config = {
list(config.keys()[0]): list(config[config.keys()[0]])
for config in yaml_config
}
json_files = ( "sourcesystem1.json",
"sourcesystem2.json",
)
for json_file in json_files:
with open(json_file, "r") as f:
sourcesystem_conf = json.load(f)
sourcesystem = sourcesystem_conf["source"]
if sourcesystem in yaml_config:
for key, value in yaml_config[sourcesystem].items():
sourcesystem_conf[key] = value
with open(json_file, "w") as f:
json.dump(sourcesystem_conf, f, indent=2)
I am getting the below error by program我通过程序收到以下错误
TypeError: 'dict_keys' object does not support indexing
When I run indivudually I get this issue for yaml
>>> yaml_config = { ... config.keys()[0]: config[config.keys()[0]] ... for config in yaml_config ... } Traceback (most recent call last): File "<stdin>", line 3, in <module> File "<stdin>", line 3, in <dictcomp> TypeError: 'dict_keys' object is not subscriptable >>>
Is there easier method to achieve my end goal where I want to replace the values in the JSON file from the Yaml configuration file是否有更简单的方法来实现我的最终目标,我想从 Yaml 配置文件中替换 JSON 文件中的值
This is needed to update 1000s of Json file in a automated way for updating it from a master Yaml file这需要以自动方式更新 1000 个 Json 文件,以便从主 Yaml 文件进行更新
The easiest way is to use pyyaml, see Jon's answer .最简单的方法是使用 pyyaml,请参阅Jon 的回答。
Then you can load you yaml file using it:然后你可以使用它加载你的 yaml 文件:
>>> import yaml
>>> yaml_config = yaml.safe_load(yaml_file)
>>> yaml_config
[{'sourcesystem1': {'MailTo': 'gokul@gmail.com', 'sourceServer': '1.2.3.500'}},
{'sourcesystem2': {'MailTo': 'gokul@gmail.com',
'sourceServer1': '2.2.3.500',
'sourceServer2': '3.2.3.500'}}]
It will be easier to manipulate a dict with source systems as keys.使用源系统作为键来操作字典会更容易。
In python 2 aDict.keys()
returns a list so the following will work:在 python 2 aDict.keys()
返回一个列表,因此以下内容将起作用:
>>> yaml_config = {
config.keys()[0]: config[config.keys()[0]]
for config in yaml_config
}
>>> yaml_config
{'sourcesystem1': {'MailTo': 'gokul@gmail.com', 'sourceServer': '1.2.3.500'},
'sourcesystem2': {'MailTo': 'gokul@gmail.com',
'sourceServer1': '2.2.3.500',
'sourceServer2': '3.2.3.500'}}
In python 3 aDict.keys()
no longer returns a list so you can simply use a for loop:在 python 3 aDict.keys()
不再返回列表,因此您可以简单地使用 for 循环:
yaml_config = {}
for config in yaml_config_raw:
source = [key for key in config][0]
yaml_config[source] = config[source]
Then you can just iterate over your json files to update them:然后你可以迭代你的 json 文件来更新它们:
import json
import yaml
with open("mast_conf.yaml", 'r') as f:
yaml_config_raw = yaml.safe_load(f)
yaml_config = {}
for config in yaml_config_raw:
source = [key for key in config][0]
yaml_config[source] = config[source]
json_files = (
"sourcesystem1.json",
"sourcesystem2.json",
)
for json_file in json_files:
with open(json_file, "r") as f:
sourcesystem_conf = json.load(f)
sourcesystem = sourcesystem_conf["source"]
if sourcesystem in yaml_config:
for key, value in yaml_config[sourcesystem].items():
sourcesystem_conf[key] = value
with open(json_file, "w") as f:
json.dump(sourcesystem_conf, f, indent=2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.