2

从 .cfg 文件读取数据后,我尝试编写 yaml 文件。转储时,文件会被转储到大量不需要的元数据。当我不使用 cfg 文件时,yaml 没有写入任何额外的标签,但是在使用 configobj 模块后它开始转储额外的元数据。

测试.cfg:-

[okd]
apiversion = v1
kind = template
    [[metadata]]
    name = xyz

Python脚本:-

from configobj import ConfigObj
import yaml

config = ConfigObj("test.cfg")

with open("test.yaml", "w") as f:
    yaml.dump(config["okd"], f)

倾倒后所需的输出:-

apiversion : v1
kind : template
metadata:
    name: xyz

正在生成的yaml:-

&id002 !!python/object/new:configobj.Section
state: !!python/tuple
- apiversion: v1
  kind: template
  metadata: &id001 !!python/object/new:configobj.Section
    state: !!python/tuple
    - name: xyz
    - _created: false
      _interpolation_engine: !!python/object:configobj.ConfigParserInterpolation
        section: *id001
      comments:
        name: []
      configspec: null
      default_values: {}
      defaults: []
      depth: 2
      extra_values: []
      inline_comments:
        name: null
      main: &id003 !!python/object/new:configobj.ConfigObj
        state: !!python/tuple
        - okd: *id002
        - BOM: false
          _created: false
          _inspec: false
          _original_configspec: null
          comments:
            okd: []
          configspec: null
          create_empty: false
          default_encoding: null
          default_values: {}
          defaults: []
          depth: 0
          encoding: null
          extra_values: []
          file_error: false
          filename: converter-tool.cfg
          final_comment:
          - ''
          indent_type: '    '
          initial_comment: []
          inline_comments:
            okd: null
          interpolation: true
          list_values: true
          main: *id003
          name: null
          newlines: '

            '
          parent: *id003
          raise_errors: false
          scalars: []
          sections:
          - okd
          stringify: true
          unrepr: false
          write_empty_values: false
      name: metadata
      parent: *id002
      scalars:
      - name
      sections: []
- _created: false
  _interpolation_engine: !!python/object:configobj.ConfigParserInterpolation
    section: *id002
  comments:
    apiversion: []
    kind: []
    metadata: []
  configspec: null
  default_values: {}
  defaults: []
  depth: 1
  extra_values: []
  inline_comments:
    apiversion: null
    kind: null
    metadata: null
  main: *id003
  name: okd
  parent: *id003
  scalars:
  - apiversion
  - kind
  sections:
  - metadata

模块版本:-

配置对象 = 5.0.6

蟒蛇3

4

0 回答 0