如何使用 ruamel.yaml 包编辑 yaml 文件,而不更改文件的缩进?
How do I edit a yaml file using ruamel.yaml package, without changing the indentation of the file?
我正在使用建议的代码 by Anthon 但它似乎不起作用。
YAML 文件:
init_config: {}
instances:
- host: <IP> # update with IP
username: <username> # update with user name
password: <password> # update with password
代码:
import ruamel.yaml
yaml = ruamel.yaml.YAML()
file_name = 'input.yaml'
config, ind, bsi = ruamel.yaml.util.load_yaml_guess_indent(open(file_name))
instances = config['instances']
instances[0]['host'] = '1.2.3.4'
instances[0]['username'] = 'Username'
instances[0]['password'] = 'Password'
with open('output.yaml', 'w') as fp:
yaml.dump(config, fp)
预期输出:
init_config: {}
instances:
- host: 1.2.3.4 # update with IP
username: Username # update with user name
password: Password # update with password
我得到的输出:
init_config: {}
instances:
- host: 1.2.3.4 # update with IP
username: Username # update with user name
password: Password # update with password
是我做错了什么,还是这个例子坏了?我怎样才能得到预期的输出?
在某处更新旧的 round_trip_dump
例程
version 4 of that answer
使用 YAML()
,我忘记将参数 indent
和 block_seq_indent
更改为
调用 .indent()
方法,如该答案的最后一个代码部分所示。
import sys
import ruamel.yaml
import ruamel.yaml
from ruamel.yaml import YAML
yaml=YAML()
file_name = 'input.yaml'
config, ind, bsi = ruamel.yaml.util.load_yaml_guess_indent(open(file_name))
instances = config['instances']
instances[0]['host'] = '1.2.3.4'
instances[0]['username'] = 'Username'
instances[0]['password'] = 'Password'
yaml.indent(mapping=ind, sequence=ind, offset=bsi) # <<<< missing
yaml.dump(config, sys.stdout)
给出:
init_config: {}
instances:
- host: 1.2.3.4 # update with IP
username: Username # update with user name
password: Password # update with password
当我回答 SO 时,我通常会制作一个 .ryd 文件。该文件既有文本段又有代码段
并且在处理时以答案的形式与代码输出的结果结合在一起
我可以粘贴到 SO 上。这样代码和输出之间就永远不会有差异。
但是那个答案是在我开始使用它之前的,我没有为
旧答案
当我更新它时。我应该...
我正在使用建议的代码
YAML 文件:
init_config: {}
instances:
- host: <IP> # update with IP
username: <username> # update with user name
password: <password> # update with password
代码:
import ruamel.yaml
yaml = ruamel.yaml.YAML()
file_name = 'input.yaml'
config, ind, bsi = ruamel.yaml.util.load_yaml_guess_indent(open(file_name))
instances = config['instances']
instances[0]['host'] = '1.2.3.4'
instances[0]['username'] = 'Username'
instances[0]['password'] = 'Password'
with open('output.yaml', 'w') as fp:
yaml.dump(config, fp)
预期输出:
init_config: {}
instances:
- host: 1.2.3.4 # update with IP
username: Username # update with user name
password: Password # update with password
我得到的输出:
init_config: {}
instances:
- host: 1.2.3.4 # update with IP
username: Username # update with user name
password: Password # update with password
是我做错了什么,还是这个例子坏了?我怎样才能得到预期的输出?
在某处更新旧的 round_trip_dump
例程
version 4 of that answer
使用 YAML()
,我忘记将参数 indent
和 block_seq_indent
更改为
调用 .indent()
方法,如该答案的最后一个代码部分所示。
import sys
import ruamel.yaml
import ruamel.yaml
from ruamel.yaml import YAML
yaml=YAML()
file_name = 'input.yaml'
config, ind, bsi = ruamel.yaml.util.load_yaml_guess_indent(open(file_name))
instances = config['instances']
instances[0]['host'] = '1.2.3.4'
instances[0]['username'] = 'Username'
instances[0]['password'] = 'Password'
yaml.indent(mapping=ind, sequence=ind, offset=bsi) # <<<< missing
yaml.dump(config, sys.stdout)
给出:
init_config: {}
instances:
- host: 1.2.3.4 # update with IP
username: Username # update with user name
password: Password # update with password
当我回答 SO 时,我通常会制作一个 .ryd 文件。该文件既有文本段又有代码段 并且在处理时以答案的形式与代码输出的结果结合在一起 我可以粘贴到 SO 上。这样代码和输出之间就永远不会有差异。
但是那个答案是在我开始使用它之前的,我没有为 旧答案 当我更新它时。我应该...