我有一个功能:
def set_localrepo(self):
stream = open("file1.yml", "r")
results = yaml.load(stream)
run_cmd = results["parameters"]["tag"]["properties"]
config_list = ( 'sleep 200', '[sh, -xc, \"echo test\' test\' >> /etc/hosts\"]')
for i in range(len(config_list)):
run_cmd.append(config_list[i])
stream.close()
with open("f2.yml", "w") as yaml_file:
yaml_file.write(yaml.dump(results, default_flow_style=False, allow_unicode=True))
yaml_file.close()
在这个文件中,我有一个 file1.yml 框架,并从那里处理列表中的内容并将其写入 f2.yml。
预期输出应如下所示:
properties:
- sleep 200
- [sh, -xc, "echo $repo_server' repo-server' >> /etc/hosts"]
但相反,它看起来像这样:
properties:
- sleep 200
- '[sh, -xc, "echo $repo_server'' repo-server'' >> /etc/hosts"]'
我已经尝试了双\、单\等的多种组合,但它可以按我的意愿工作。
请告知可以做些什么来解决这个问题。我怀疑它与 YAML 转储实用程序和转义字符组合有关。
感谢期待!