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

  • SEARCH
  • Home
  • 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 7008239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:42:33+00:00 2026-05-27T21:42:33+00:00

How do you control how the order in which PyYaml outputs key/value pairs when

  • 0

How do you control how the order in which PyYaml outputs key/value pairs when serializing a Python dictionary?

I’m using Yaml as a simple serialization format in a Python script. My Yaml serialized objects represent a sort of “document”, so for maximum user-friendliness, I’d like my object’s “name” field to appear first in the file. Of course, since the value returned by my object’s __getstate__ is a dictionary, and Python dictionaries are unordered, the “name” field will be serialized to a random location in the output.

e.g.

>>> import yaml
>>> class Document(object):
...     def __init__(self, name):
...         self.name = name
...         self.otherstuff = 'blah'
...     def __getstate__(self):
...         return self.__dict__.copy()
... 
>>> doc = Document('obj-20111227')
>>> print yaml.dump(doc, indent=4)
!!python/object:__main__.Document
otherstuff: blah
name: obj-20111227
  • 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-05-27T21:42:34+00:00Added an answer on May 27, 2026 at 9:42 pm

    Took me a few hours of digging through PyYAML docs and tickets, but I eventually discovered this comment that lays out some proof-of-concept code for serializing an OrderedDict as a normal YAML map (but maintaining the order).

    e.g. applied to my original code, the solution looks something like:

    >>> import yaml
    >>> from collections import OrderedDict
    >>> def dump_anydict_as_map(anydict):
    ...     yaml.add_representer(anydict, _represent_dictorder)
    ... 
    >>> def _represent_dictorder( self, data):
    ...     if isinstance(data, Document):
    ...         return self.represent_mapping('tag:yaml.org,2002:map', data.__getstate__().items())
    ...     else:
    ...         return self.represent_mapping('tag:yaml.org,2002:map', data.items())
    ... 
    >>> class Document(object):
    ...     def __init__(self, name):
    ...         self.name = name
    ...         self.otherstuff = 'blah'
    ...     def __getstate__(self):
    ...         d = OrderedDict()
    ...         d['name'] = self.name
    ...         d['otherstuff'] = self.otherstuff
    ...         return d
    ... 
    >>> dump_anydict_as_map(Document)
    >>> doc = Document('obj-20111227')
    >>> print yaml.dump(doc, indent=4)
    !!python/object:__main__.Document
    name: obj-20111227
    otherstuff: blah
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any (simple) way to get some control of the order in which
Is there any way to control the order of which the GridView control renders
I'm using Django forms in my website and would like to control the order
I'm using primefaces and their growl control in order to display validation error messages
I'm looking for a class in java that has key-value association, but without using
i want to run and control PSFTP from a Python script in order to
Can I control the order static objects are being destructed? Is there any way
I can use the MS Word ActiveX control in order to access some parameters
In order for the user to control the volume , my android application has
I have a segmented control where the user can select how to order a

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.