__________     __ __     __  _______    ________
  / ____/ __ \   / // /    / / / /  _/ |  / / ____/
 / / __/ / / /  / // /_   / /_/ // / | | / / __/
/ /_/ / /_/ /  /__  __/  / __  // /  | |/ / /___
\____/\____/     /_/    /_/ /_/___/  |___/_____/

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Using YAML in python

BY: @ruttydm | CREATED: May 20, 2017, 7:32 p.m. | VOTES: 2 | PAYOUT: $0.02 | [ VOTE ]

YAML stands for "YAML Ain't Markup Language" and is mostly used in configuration files.
YAML, in contrary to JSON, is made to be very readable
and is not designed to be used for api's or other communication protocols. This is because the parsing of a YAML file requires the computer
a little bit more effort than parsing a JSON file.

Installing PyYAML

To use YAML in python you have to install the PyYAML module by doing:

pip install PyYAML

Or if you are using pip3:

pip3 install PyYAML

Or when you are on linux (like me):

sudo pip3 install PyYAML

Loading YAML from a file

If you want to parse a YAML file.

import the yaml module

import yaml

load the yaml file

document = file('document.yaml', 'r')

and finally parse the file

parsed = yaml.load(stream)

You can simply revert it back to a YAML format by doing:

print(yaml.dump(parsed))

Loading YAML from a string

This is very similar as loading YAML from a file

yaml.load("""
none: [~, null]
bool: [true, false, on, off]
int: 42
float: 3.14159
list: [LITE, RES_ACID, SUS_DEXT]
dict: {hp: 13, sp: 5}
""")

TAGS: [ #python ] [ #programming ]

Replies

@steemitboard | May 24, 2017, 11:10 p.m. | Votes: 0 | [ VOTE ]

Congratulations @ruttydm! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[IMAGE: https://steemitimages.com/70x80/http://steemitboard.com/notifications/commented.png] Award for the number of comments received

Click on any badge to view your own Board of Honnor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

If you want to support the SteemitBoard project, your upvote for this notification is welcome!

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>