[PYTHON] How to disable the reference feature when outputting PyYaml

If you do yaml.dump without setting anything, the reference function will be used. It may not be necessary.

import yaml


person = {
    "name": "foo",
    "age": 20
}
d = [person, person]

print(yaml.dump(d))

For example, the output is as follows.

- &id001 {age: 20, name: foo}
- *id001

You can pass the Dumper option to yaml.dump. Then, the ignore_aliases () method of the Dumper class passed here should return True.

class IgnoreReferenceDumper(yaml.Dumper):
    def ignore_aliases(self, data):
        return True

print(yaml.dump(d, Dumper=IgnoreReferenceDumper))

Peace has come.

- {age: 20, name: foo}
- {age: 20, name: foo}

Recommended Posts

How to disable the reference feature when outputting PyYaml
How to change the behavior when loading / dumping yaml with PyYAML and its details
How to use the generator
How to use the decorator
How to increase the axis
How to start the program
How to calculate the autocorrelation coefficient
How to use the zip function
How to use the optparse module
The 15th offline real-time how to write reference problem in Python
How to read the SNLI dataset
How to get the Python version
How to add sudo when debugging
[Linux] How to disable the automatic update of the /etc/resolv.conf file (AmazonLinux2)
[Python] How to import the library
How to get the vertex coordinates of a feature in ArcPy
How to overwrite the output to the console
The 14th offline real-time how to write reference problem in python
How to use the ConfigParser module
The 18th offline real-time how to write reference problem in Python
[PostgreSQL] How to grant superuser authority when the user (role) with superuser authority is 0
The 17th offline real-time how to write reference problem implemented in Python
How to hide the command prompt when running python in visual studio 2015
The 16th offline real-time how to write reference problem to solve with Python
The 19th offline real-time how to write reference problem to solve with Python
How to automatically notify by phone when the python system is down
How to display the progress bar (tqdm)
How to use the Spark ML pipeline
How to check the version of Django
How to solve the bin packing problem
How to get IP when Tornado + nginx
How to set the server time to Japanese time
How to manually update the AMP cache
[Linux] How to use the echo command
How to use the Linux grep command
How to get colored output to the console
How to operate Linux from the console
How to access the Datastore from the outside
How to use the IPython debugger (ipdb)
How to deal with the problem that Japanese characters are garbled when outputting logs using JSON log formatter
How to display in the entire window when setting the background image with tkinter
How to add pre-save processing when adding objects on the Django admin site
How to calculate the volatility of a brand
How to start Python (Flask) when EC2 starts
How to use the C library in Python
How to find the area of the Voronoi diagram
How to use MkDocs for the first time
How to deal with errors when hitting pip ②
How to specify the NIC to scan with amazon-dash
[Django] How to resolve errors when installing mysqlclient
When you want to update the chrome driver.
How to try the friends-of-friends algorithm with pyfof
How to use the graph drawing library Bokeh
How to deal with SessionNotCreatedException when using Selenium
How to print debug messages to the Django console
How to use the Google Cloud Translation API
How to operate Linux from the outside Procedure
How to fix when Terminal input becomes abnormal
How to use the NHK program guide API
How to remember when you forget a word
[Algorithm x Python] How to use the list