Home > database >  Adding qoute on YAML file from a dictionary
Adding qoute on YAML file from a dictionary

Time:08-12

I have the following kind of dictionary:

my_dict

my_dict = {
'table':
           { 
    'description': 'Table 1',
        
    'columns': [
        {'name': 'Column1',
         'description': 'first column'
                },

    {'name': 'Column2',
     'description': 'second column'
    },
  
    {'name': 'Column3',
     'description': 'third column'
    }
   ]
 }
}

I want to write this dictionary and get the YAML file using the following script.

with open('Table1.yaml', 'w') as f:
      yaml.dump(my_dict, f)

The result that I got on the YAML file looks like the following:

table:
  description: Table 1
  columns:
  - name: Column1
    description: first column
  - name: Column2
    description: second column
  - name: Column3
    description: third column

But I would like to get the description value in the quote below:

table:
  description: 'Table 1'
  columns:
  - name: Column1
    description: 'first column'
  - name: Column2
    description: 'second column'
  - name: Column3
    description: 'third column'

Can anyone help on how to add the quotes for every description key value?

CodePudding user response:

Set the default_style parameter.

yaml.dump(my_dict, f, default_style="'")

You can read about the other parameters for example at https://realpython.com/python-yaml/#dumping-python-objects-to-yaml-documents.

Complete working example:

import yaml

my_dict = {
    'table':
    {
        'description': 'Table 1',

        'columns': [
            {'name': 'Column1',
             'description': 'first column'
             },

            {'name': 'Column2',
             'description': 'second column'
             },

            {'name': 'Column3',
             'description': 'third column'
             }
        ]
    }
}

with open('Table1.yaml', 'w') as f:
    yaml.dump(my_dict, f, default_style="'")

CodePudding user response:

Try:

import sys
import yaml

my_dict = {
    "table": {
        "description": "Table 1",
        "columns": [
            {"name": "Column1", "description": "first column"},
            {"name": "Column2", "description": "second column"},
            {"name": "Column3", "description": "third column"},
        ],
    }
}


class SingleQuoted(str):
    pass


def single_quoted_presenter(dumper, data):
    return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="'")


yaml.add_representer(SingleQuoted, single_quoted_presenter)


def update_description(o):
    if isinstance(o, dict):
        for k, v in o.items():
            if k == "description" and isinstance(v, str):
                o[k] = SingleQuoted(v)
            else:
                update_description(v)
    if isinstance(o, list):
        for v in o:
            update_description(v)


update_description(my_dict)
yaml.dump(my_dict, sys.stdout, sort_keys=False, default_flow_style=False)

Prints:

table:
  description: 'Table 1'
  columns:
  - name: Column1
    description: 'first column'
  - name: Column2
    description: 'second column'
  - name: Column3
    description: 'third column'
  • Related