在YAML文件中,如何在Python中打印[]而不使用字符串

在YAML文件中,如何在Python中打印[]而不使用字符串,python,yaml,pyyaml,Python,Yaml,Pyyaml,在我的yam文件中,我正在尝试这个 with open(fname, "w") as f: yaml.safe_dump({'items':['test', 'test2']}, f, default_flow_style=False, width=50, indent=4) 它以下面的格式打印 items: - 'test' - 'test2' 我希望输出的格式如下 items: ['test', 'test2'] 我该怎么做 编辑: 这

在我的yam文件中,我正在尝试这个

with open(fname, "w") as f:
     yaml.safe_dump({'items':['test', 'test2']}, f,
                    default_flow_style=False, width=50, indent=4)
它以下面的格式打印

items:
- 'test'
- 'test2'
我希望输出的格式如下

items: ['test', 'test2']
我该怎么做

编辑:

这是我的完整代码

   d = {}        
   for m in ['B1', 'B2', 'B3']:
                d2 = {}
                for f in ['A1', 'A2', 'A3']:
                    # here i don't want any flow style
                    d2[f] = ['test', 'test2']
                d[m] = d2

    with open(fname, "w") as f:
        yaml.safe_dump(d, f, default_flow_style=True, width=50, indent=8)

不要将
default\u flow\u style=False放入,然后,:

对于部分文档格式,您可以使用自定义设置,例如:


如果您想要精细控制,并且只有具有流样式的特定列表,您应该使用(这是我的PyYAML增强版):

将为您提供:

B1:
        A1: [test, test2]
        A3: [test, test2]
        A2:
        - test
        - test2
B2:
        A1: [test, test2]
        A3: [test, test2]
        A2:
        - test
        - test2
B3:
        A1: [test, test2]
        A3: [test, test2]
        A2:
        - test
        - test2
CommentedSeq
的行为类似于普通的Python列表,但允许您指定注释、设置流/块样式等

ruamel.yaml
通常用于在往返yaml时保留元素上的注释、流/块样式等。即,如果您要附加:

d2 = ruamel.yaml.load(x, Loader=ruamel.yaml.RoundTripLoader)
y = ruamel.yaml.dump(
    d2, Dumper=ruamel.yaml.RoundTripDumper, width=50, indent=8)
assert x == y
这个断言成立


但它当然也可以用来从头开始生成YAML。例如,您也可以使用CommentedMap类型并保持dict/映射键的有序。

如果您想要默认的流样式,为什么要添加
default\u flow\u style=False
呢?@abarnert我有嵌套的dict,我不想要一个dict的默认流样式,但我想要其他dict的默认流样式。是否可能重复您希望YAML文件中的所有列表都是流样式,还是仅特定列表?我有嵌套的dict,我不希望只对一个dict使用默认流样式,但希望对其他dict使用默认流样式。它是possible@user3214546提供一个
stream=fp
dump()
,其中fp是一个打开的文件指针(
,open('somefile.yaml','w')作为fp:
)。如果
stream
None
则“dump”将作为字符串返回。我正在使用您的库。我有没有办法在文件的顶部添加注释,比如描述文件的用途keys@user3214546是的,有,因为如果您往返的话,这些文件开头的注释会被保留。每一条评论都是自己的一行,所以它永远不会只是替换一行。你可以发布一个新的问题,清楚地说明你从什么开始(顶部有注释的文件需要更改(如果有),顶部没有注释的文件,需要插入什么(一个注释行,多个注释行)。如果需要,请点击此处,我将尽快回答。我已在此处添加了问题,如果我使用
CommentedSeq([“test”])
则输出为
[test]
如何获得
['test']
import ruamel.yaml
from ruamel.yaml.comments import CommentedSeq

d = {}
for m in ['B1', 'B2', 'B3']:
    d2 = {}
    for f in ['A1', 'A2', 'A3']:
        # here i don't want any flow style
        d2[f] = CommentedSeq(['test', 'test2'])
        if f != 'A2':
            d2[f].fa.set_flow_style()
    d[m] = d2

x = ruamel.yaml.dump(
    d, Dumper=ruamel.yaml.RoundTripDumper,
    default_flow_style=False, width=50, indent=8)
print(x)
B1:
        A1: [test, test2]
        A3: [test, test2]
        A2:
        - test
        - test2
B2:
        A1: [test, test2]
        A3: [test, test2]
        A2:
        - test
        - test2
B3:
        A1: [test, test2]
        A3: [test, test2]
        A2:
        - test
        - test2
d2 = ruamel.yaml.load(x, Loader=ruamel.yaml.RoundTripLoader)
y = ruamel.yaml.dump(
    d2, Dumper=ruamel.yaml.RoundTripDumper, width=50, indent=8)
assert x == y