很难简明扼要地描述,所以我来演示一下.

from sys import stdout
from ruamel.yaml import YAML
yml = YAML()
doc = """
kind: ImageSetConfiguration
apiVersion: mirror.openshift.io/v1alpha2
storageConfig:
  local:
    path: ./operator-images
mirror:
  operators:
  - catalog: registry.redhat.io/redhat/redhat-operator-index:v4.14
    packages:
    - name: rhods-operator
      channels:
      - name: fast
        minVersion: 2.7.0
        maxVersion: 2.7.0
    - name: nfd
      channels:
      - name: stable
  additionalImages:
  - name: quay.io/integreatly/prometheus-blackbox-exporter@sha256:35b9d2c1002201723b7f7a9f54e9406b2ec4b5b0f73d114f47c70e15956103b5
  - name: quay.io/modh/codeserver@sha256:7b53d6c49b0e18d8907392c19b23ddcdcd4dbf730853ccdf153358ca81b2c523
  - name: quay.io/modh/cuda-notebooks@sha256:00c53599f5085beedd0debb062652a1856b19921ccf59bd76134471d24c3fa7d
  - name: quay.io/modh/cuda-notebooks@sha256:4275eefdab2d5e32a7be26f747d1cdb58e82fb0cd57dda939a9a24e084bd1f7e
  - name: quay.io/modh/cuda-notebooks@sha256:6fadedc5a10f5a914bb7b27cd41bc644392e5757ceaf07d930db884112054265
  - name: quay.io/modh/cuda-notebooks@sha256:88d80821ff8c5d53526794261d519125d0763b621d824f8c3222127dab7b6cc8
"""
y = yml.load(doc)
yml.dump(y, stdout)

打印内容如下:

kind: ImageSetConfiguration
apiVersion: mirror.openshift.io/v1alpha2
storageConfig:
  local:
    path: ./operator-images
mirror:
  operators:
  - catalog: registry.redhat.io/redhat/redhat-operator-index:v4.14
    packages:
    - name: rhods-operator
      channels:
      - name: fast
        minVersion: 2.7.0
        maxVersion: 2.7.0
    - name: nfd
      channels:
      - name: stable
  additionalImages:
  - name: 
      quay.io/integreatly/prometheus-blackbox-exporter@sha256:35b9d2c1002201723b7f7a9f54e9406b2ec4b5b0f73d114f47c70e15956103b5
  - name: 
      quay.io/modh/codeserver@sha256:7b53d6c49b0e18d8907392c19b23ddcdcd4dbf730853ccdf153358ca81b2c523
  - name: 
      quay.io/modh/cuda-notebooks@sha256:00c53599f5085beedd0debb062652a1856b19921ccf59bd76134471d24c3fa7d
  - name: 
      quay.io/modh/cuda-notebooks@sha256:4275eefdab2d5e32a7be26f747d1cdb58e82fb0cd57dda939a9a24e084bd1f7e
  - name: 
      quay.io/modh/cuda-notebooks@sha256:6fadedc5a10f5a914bb7b27cd41bc644392e5757ceaf07d930db884112054265
  - name: 
      quay.io/modh/cuda-notebooks@sha256:88d80821ff8c5d53526794261d519125d0763b621d824f8c3222127dab7b6cc8

为什么在转储时,mirror.additionalImages列表中的name: xxx个单词会被分成两行?它是基于密钥值的长度还是别的什么?我怎么才能阻止它呢?(在一个有数百个这样的文件中,这真的变得很荒谬.)我已经try 了YAML()个实例的各种设置,但都没有用.(例如,indent()compact()default_flow_style.)

推荐答案

YAML实例中输出的默认宽度是80个字符,这就是导致值(太长)的原因, 要绕到下一行.

如果您将yaml.width设置为更大的值,则不会发生这种情况:

from sys import stdout
from ruamel.yaml import YAML

yaml = YAML()
yaml.width = 1024     # added line
doc = """
kind: ImageSetConfiguration
apiVersion: mirror.openshift.io/v1alpha2
storageConfig:
  local:
    path: ./operator-images
mirror:
  operators:
  - catalog: registry.redhat.io/redhat/redhat-operator-index:v4.14
    packages:
    - name: rhods-operator
      channels:
      - name: fast
        minVersion: 2.7.0
        maxVersion: 2.7.0
    - name: nfd
      channels:
      - name: stable
  additionalImages:
  - name: quay.io/integreatly/prometheus-blackbox-exporter@sha256:35b9d2c1002201723b7f7a9f54e9406b2ec4b5b0f73d114f47c70e15956103b5
  - name: quay.io/modh/codeserver@sha256:7b53d6c49b0e18d8907392c19b23ddcdcd4dbf730853ccdf153358ca81b2c523
  - name: quay.io/modh/cuda-notebooks@sha256:00c53599f5085beedd0debb062652a1856b19921ccf59bd76134471d24c3fa7d
  - name: quay.io/modh/cuda-notebooks@sha256:4275eefdab2d5e32a7be26f747d1cdb58e82fb0cd57dda939a9a24e084bd1f7e
  - name: quay.io/modh/cuda-notebooks@sha256:6fadedc5a10f5a914bb7b27cd41bc644392e5757ceaf07d930db884112054265
  - name: quay.io/modh/cuda-notebooks@sha256:88d80821ff8c5d53526794261d519125d0763b621d824f8c3222127dab7b6cc8
"""
y = yaml.load(doc)
yaml.dump(y, stdout)

它给出:

kind: ImageSetConfiguration
apiVersion: mirror.openshift.io/v1alpha2
storageConfig:
  local:
    path: ./operator-images
mirror:
  operators:
  - catalog: registry.redhat.io/redhat/redhat-operator-index:v4.14
    packages:
    - name: rhods-operator
      channels:
      - name: fast
        minVersion: 2.7.0
        maxVersion: 2.7.0
    - name: nfd
      channels:
      - name: stable
  additionalImages:
  - name: quay.io/integreatly/prometheus-blackbox-exporter@sha256:35b9d2c1002201723b7f7a9f54e9406b2ec4b5b0f73d114f47c70e15956103b5
  - name: quay.io/modh/codeserver@sha256:7b53d6c49b0e18d8907392c19b23ddcdcd4dbf730853ccdf153358ca81b2c523
  - name: quay.io/modh/cuda-notebooks@sha256:00c53599f5085beedd0debb062652a1856b19921ccf59bd76134471d24c3fa7d
  - name: quay.io/modh/cuda-notebooks@sha256:4275eefdab2d5e32a7be26f747d1cdb58e82fb0cd57dda939a9a24e084bd1f7e
  - name: quay.io/modh/cuda-notebooks@sha256:6fadedc5a10f5a914bb7b27cd41bc644392e5757ceaf07d930db884112054265
  - name: quay.io/modh/cuda-notebooks@sha256:88d80821ff8c5d53526794261d519125d0763b621d824f8c3222127dab7b6cc8

Python相关问答推荐

运行Python脚本时,用作命令行参数的SON文本

两个pandas的平均值按元素的结果串接元素.为什么?

django禁止直接分配到多对多集合的前端.使用user.set()

如何在solve()之后获得症状上的等式的值

将9个3x3矩阵按特定顺序排列成9x9矩阵

Stacked bar chart from billrame

索引到 torch 张量,沿轴具有可变长度索引

为什么\b在这个正则表达式中不解释为反斜杠

如何在达到end_time时自动将状态字段从1更改为0

手动设置seborn/matplotlib散点图连续变量图例中显示的值

在输入行运行时停止代码

为什么'if x is None:pass'比'x is None'单独使用更快?

根据客户端是否正在传输响应来更改基于Flask的API的行为

Beautifulsoup:遍历一个列表,从a到z,并解析数据,以便将其存储在pdf中.

Python如何导入类的实例

Polars表达式无法访问中间列创建表达式

上传文件并使用Panda打开时的Flask 问题

关于数字S种子序列内部工作原理的困惑

ValueError:必须在Pandas 中生成聚合值

用LAKEF划分实木地板AWS Wrangler