Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:02:18+00:00 2026-06-14T06:02:18+00:00

I use PyYAML to output a python dictionary to YAML format: import yaml d

  • 0

I use PyYAML to output a python dictionary to YAML format:

import yaml
d = { 'bar': { 'foo': 'hello', 'supercalifragilisticexpialidocious': 'world' } }
print yaml.dump(d, default_flow_style=False)

The output is:

bar:
  foo: hello
  supercalifragilisticexpialidocious: world

But I would like:

bar:
  foo                                : hello
  supercalifragilisticexpialidocious : world

Is there a simple solution to that problem, even a suboptimal one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:02:20+00:00Added an answer on June 14, 2026 at 6:02 am

    Ok, here is what I’ve come up with so far.

    My solution involves two steps. The first step defines a dictionary representer for adding trailing spaces to keys. With this step, I obtain quoted keys in the output. This is why I add a second step for removing all these quotes:

    import yaml
    d = {'bar': {'foo': 'hello', 'supercalifragilisticexpialidocious': 'world'}}
    
    
    # FIRST STEP:
    #   Define a PyYAML dict representer for adding trailing spaces to keys
    
    def dict_representer(dumper, data):
        keyWidth = max(len(k) for k in data)
        aligned = {k+' '*(keyWidth-len(k)):v for k,v in data.items()}
        return dumper.represent_mapping('tag:yaml.org,2002:map', aligned)
    
    yaml.add_representer(dict, dict_representer)
    
    
    # SECOND STEP:
    #   Remove quotes in the rendered string
    
    print(yaml.dump(d, default_flow_style=False).replace('\'', ''))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use PyYAML to work with YAML files. I wonder how I can properly
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
use 5.010; use strict; use warnings; use JSON::XS; use YAML::XS; my %data = ();
use Rack::Session::Pool ... session[:msg]=Hello Rack EDIT: The word session doesn't seem to resolve. I
use strict; use warnings; open(FILE1, /cygdrive/c/cpros/mola.txt); my $line = <FILE1>; print $line; close(FILE1); open(FILE1,
use strict; use warnings; open(FILE1,/cygdrive/c/cpros/karthik/molk.txt); my $line = < FILE1 > ; print $line
I am passing Yaml created with PyYaml to SnakeYaml and Snakeyaml does not seem
use Control::CLI; $cli = new Control::CLI('SSH'); $cli->connect(Host=>'10.10.10.10',Username=>'user',Password=>'pwd'); $cli->waitfor('>'); $cli->print('Show XXXXXXXXXXXXXXXXXXXX| grep Active'); @f=$cli->waitfor('>'); print
GAE uses PyYAML to deal with YAML files. I see that library in GAE
use Image::Imlib2; my $a = Image::Imlib2->load(/foo/file); gives me the following error: Runtime error: Image::Imlib2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.