我在 linux 和 osx 上运行了一个简单的 python 2.7.12 脚本,提供相同的输出。在转储期间使用 ruamel 时,布尔值似乎转换为全小写。作为将字段转换为不带引号的字符串时的测试,没有大小写转换。
文件:test.yml
namespace: default
testapp_appname: testapp
images:
testapp:
path: "foo/testapp"
tag: "202120810083053-1.2.3"
testapp_replicas: 1
testapp_monitoring_enabled: False
testapp_node_selector: ""
我的简单测试脚本:
import ruamel
import sys
from ruamel.yaml import YAML
yaml = YAML()
def main():
config_path = "test.yml"
image = "testapp"
timestamp = "202120810083053-"
version = "1.2.3"
config = ruamel.yaml.round_trip_load(open(config_path), preserve_quotes=True)
config['images'][image]['tag'] = "{}{}".format(timestamp, version)
ruamel.yaml.round_trip_dump(config, sys.stdout)
if __name__ == "__main__":
main()
输入:
testapp_monitoring_enabled: False
输出:
testapp_monitoring_enabled: false