Removing yaml tags from yaml.dump()

Viewed 380

Am trying to write a yaml file after reading data from a .cfg file. While dumping, the file get dumped with huge metadata that is not desired. When I wasn't using the cfg file, the yaml was not writing any extra tags but after using the configobj module it started dumping extra metadata.

test.cfg:-

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

Python script:-

from configobj import ConfigObj
import yaml

config = ConfigObj("test.cfg")

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

Desired output after dumping:-

apiversion : v1
kind : template
metadata:
    name: xyz

yaml that is getting generated:-

&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

Versions of modules:-

configobj = 5.0.6

python3

0 Answers
Related