以特定格式将元组转储到 YAML 文件中
Dump tuple into YAML file in a specific format
我有一个特定的格式,我想将元组转储到 YAML 文件中。我有以下代码,我已经尝试 运行 将元组转储到 YAML 文件中。从下面的代码中,我尝试使用 append() 将一对元组 (x, y) 添加到列表中。之后,我会将列表转储回 YAML 文件中。我的问题是,当我转储回 YAML 文件时,我是否可以保留文件的相同特定格式?
import ruamel.yaml
def addObstacles():
yaml = ruamel.yaml.YAML()
with open('input.yaml') as f:
doc = yaml.load(f)
x = 5
y = 6
doc['map']['obstacles'].append(list((x,y)))
with open('input.yaml', 'w') as f:
yaml.dump(doc, f)
以下文件的输出结果如下:
map:
dimensions: [8, 8]
obstacles:
- !!python/tuple [4, 5]
- !!python/tuple [2, 0]
- !!python/tuple [1, 1]
- !!python/tuple [0, 5]
- !!python/tuple [2, 5]
- !!python/tuple [4, 4]
- !!python/tuple [7, 5]
- !!python/tuple [1, 4]
- !!python/tuple [6, 7]
- !!python/tuple [6, 3]
- !!python/tuple [1, 7]
- !!python/tuple [3, 6]
- - 5
- 6
文件的预期输出如下:
map:
dimensions: [8, 8]
obstacles:
- !!python/tuple [4, 5]
- !!python/tuple [2, 0]
- !!python/tuple [1, 1]
- !!python/tuple [0, 5]
- !!python/tuple [2, 5]
- !!python/tuple [4, 4]
- !!python/tuple [7, 5]
- !!python/tuple [1, 4]
- !!python/tuple [6, 7]
- !!python/tuple [6, 3]
- !!python/tuple [1, 7]
- !!python/tuple [3, 6]
- !!python/tuple [5, 6]
我不确定你是否真的需要 ruamel.yaml
。 PyYAML 开箱即用:
import yaml
def addObstacles():
with open('in.yaml') as f:
doc = yaml.load(f)
doc['map']['obstacles'].append((5, 6))
with open('out.yaml', 'w') as f:
yaml.dump(doc, f)
既然你追加了一个列表,你就会得到一个列表,但我假设你试过了,因为一个普通的元组会
不起作用,因为您实际上在 ruamel.yaml.
中发现了一个错误
这可以通过两行代码轻松解决,并通过指定 default_flow_style
:
import sys
import ruamel.yaml
if ruamel.yaml.version_info < (0, 16, 7):
ruamel.yaml.representer.RoundTripRepresenter.add_representer(tuple,
ruamel.yaml.representer.Representer.represent_tuple)
def addObstacles():
yaml = ruamel.yaml.YAML()
yaml.default_flow_style = None # default is False, which would get you a block sequence
with open('input.yaml') as f:
doc = yaml.load(f)
doc['map']['obstacles'].append((5, 6))
yaml.dump(doc, sys.stdout)
addObstacles()
这样你就能得到你想要的:
map:
dimensions: [8, 8]
obstacles:
- !!python/tuple [4, 5]
- !!python/tuple [2, 0]
- !!python/tuple [1, 1]
- !!python/tuple [0, 5]
- !!python/tuple [2, 5]
- !!python/tuple [4, 4]
- !!python/tuple [7, 5]
- !!python/tuple [1, 4]
- !!python/tuple [6, 7]
- !!python/tuple [6, 3]
- !!python/tuple [1, 7]
- !!python/tuple [3, 6] # add one after this
- !!python/tuple [5, 6]
(我在我使用的 YAML 输入文件中添加了注释)。
我有一个特定的格式,我想将元组转储到 YAML 文件中。我有以下代码,我已经尝试 运行 将元组转储到 YAML 文件中。从下面的代码中,我尝试使用 append() 将一对元组 (x, y) 添加到列表中。之后,我会将列表转储回 YAML 文件中。我的问题是,当我转储回 YAML 文件时,我是否可以保留文件的相同特定格式?
import ruamel.yaml
def addObstacles():
yaml = ruamel.yaml.YAML()
with open('input.yaml') as f:
doc = yaml.load(f)
x = 5
y = 6
doc['map']['obstacles'].append(list((x,y)))
with open('input.yaml', 'w') as f:
yaml.dump(doc, f)
以下文件的输出结果如下:
map:
dimensions: [8, 8]
obstacles:
- !!python/tuple [4, 5]
- !!python/tuple [2, 0]
- !!python/tuple [1, 1]
- !!python/tuple [0, 5]
- !!python/tuple [2, 5]
- !!python/tuple [4, 4]
- !!python/tuple [7, 5]
- !!python/tuple [1, 4]
- !!python/tuple [6, 7]
- !!python/tuple [6, 3]
- !!python/tuple [1, 7]
- !!python/tuple [3, 6]
- - 5
- 6
文件的预期输出如下:
map:
dimensions: [8, 8]
obstacles:
- !!python/tuple [4, 5]
- !!python/tuple [2, 0]
- !!python/tuple [1, 1]
- !!python/tuple [0, 5]
- !!python/tuple [2, 5]
- !!python/tuple [4, 4]
- !!python/tuple [7, 5]
- !!python/tuple [1, 4]
- !!python/tuple [6, 7]
- !!python/tuple [6, 3]
- !!python/tuple [1, 7]
- !!python/tuple [3, 6]
- !!python/tuple [5, 6]
我不确定你是否真的需要 ruamel.yaml
。 PyYAML 开箱即用:
import yaml
def addObstacles():
with open('in.yaml') as f:
doc = yaml.load(f)
doc['map']['obstacles'].append((5, 6))
with open('out.yaml', 'w') as f:
yaml.dump(doc, f)
既然你追加了一个列表,你就会得到一个列表,但我假设你试过了,因为一个普通的元组会 不起作用,因为您实际上在 ruamel.yaml.
中发现了一个错误这可以通过两行代码轻松解决,并通过指定 default_flow_style
:
import sys
import ruamel.yaml
if ruamel.yaml.version_info < (0, 16, 7):
ruamel.yaml.representer.RoundTripRepresenter.add_representer(tuple,
ruamel.yaml.representer.Representer.represent_tuple)
def addObstacles():
yaml = ruamel.yaml.YAML()
yaml.default_flow_style = None # default is False, which would get you a block sequence
with open('input.yaml') as f:
doc = yaml.load(f)
doc['map']['obstacles'].append((5, 6))
yaml.dump(doc, sys.stdout)
addObstacles()
这样你就能得到你想要的:
map:
dimensions: [8, 8]
obstacles:
- !!python/tuple [4, 5]
- !!python/tuple [2, 0]
- !!python/tuple [1, 1]
- !!python/tuple [0, 5]
- !!python/tuple [2, 5]
- !!python/tuple [4, 4]
- !!python/tuple [7, 5]
- !!python/tuple [1, 4]
- !!python/tuple [6, 7]
- !!python/tuple [6, 3]
- !!python/tuple [1, 7]
- !!python/tuple [3, 6] # add one after this
- !!python/tuple [5, 6]
(我在我使用的 YAML 输入文件中添加了注释)。