Questions tagged [pyyaml]

PyYAML is a YAML 1.1 parser and emitter for Python. Use this tag for PyYAML specific python questions.

PyYAML is a parser/dumper for YAML 1.1 for the Python programming language.

PyYAML does not yet implemented the feature changes from the YAML 1.2 specification released in 2009.

See http://pyyaml.org/wiki/PyYAML for background and documentation.

Source code: https://github.com/yaml/pyyaml

828 questions
48
votes
3 answers

YAML loads 5e-6 as string and not a number

When I load a number with e form a JSON dump with YAML, the number is loaded as a string and not a float. I think this simple example can explain my problem. import json import yaml In [1]: import json In [2]: import yaml In [3]: All =…
Oren
  • 4,711
  • 4
  • 37
  • 63
39
votes
4 answers

How can I output blank value in python yaml file

I am writing yaml file like this with open(fname, "w") as f: yaml.safe_dump({'allow':'', 'deny': ''}, f, default_flow_style=False, width=50, indent=4) This outputs: allow: '' I want to output as allow: How can I do that?
user3214546
  • 6,523
  • 13
  • 51
  • 98
38
votes
1 answer

Dump in PyYaml as utf-8

I'm trying to load a bunch of utf-8 encoded strings and dump them again with PyYaml. This is the code for loading/dumping: lang_yml = yaml.load(codecs.open(lang + ".yml.old", "r", "utf-8")) test_file_path = lang + '.yml' stream =…
panmari
  • 3,627
  • 3
  • 28
  • 48
37
votes
5 answers

How to read a python tuple using PyYAML?

I have the following YAML file named input.yaml: cities: 1: [0,0] 2: [4,0] 3: [0,4] 4: [4,4] 5: [2,2] 6: [6,2] highways: - [1,2] - [1,3] - [1,5] - [2,4] - [3,4] - [5,4] start: 1 end: 4 I'm loading it using PyYAML and…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
35
votes
2 answers

pyyaml is producing undesired !!python/unicode output

I am using pyyaml to dump an object to a file. There are several unicode strings in the object. I've done this before, but now it's producing output items like this: 'item': !!python/unicode "some string" Instead of the desired: 'item': 'some…
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
33
votes
2 answers

How to force PyYAML to load strings as unicode objects?

The PyYAML package loads unmarked strings as either unicode or str objects, depending on their content. I would like to use unicode objects throughout my program (and, unfortunately, can't switch to Python 3 just yet). Is there an easy way to force…
Petr Viktorin
  • 65,510
  • 9
  • 81
  • 81
33
votes
3 answers

Performing arithmetic operation in YAML?

Sometimes I have to specify the time (in seconds) in the configuration file, and it's quite annoying to write exact seconds amount - instead I would like to perform arithmetics so I could use: some_time: 1 * 24 * 60 * 60 instead of…
Lucas
  • 3,517
  • 13
  • 46
  • 75
30
votes
3 answers

Any yaml libraries in Python that support dumping of long strings as block literals or folded blocks?

I'd like to be able to dump a dictionary containing long strings that I'd like to have in the block style for readability. For example: foo: | this is a block literal bar: > this is a folded block PyYAML supports the loading of documents…
guidoism
  • 7,820
  • 8
  • 41
  • 59
29
votes
4 answers

Parsing YAML, return with line number

I'm making a document generator from YAML data, which would specify which line of the YAML file each item is generated from. What is the best way to do this? So if the YAML file is like this: - key1: item 1 key2: item 2 - key1: another item 1 …
puzzlet
  • 390
  • 3
  • 11
27
votes
6 answers

How to replace environment variable value in yaml file to be parsed using python script

I need to use environment variable "PATH" in yaml file which needs to be parsed with a script. This is the environment variable I have set on my terminal: $ echo $PATH /Users/abc/Downloads/tbwork This is my sample.yml: --- Top:…
npatel
  • 1,081
  • 2
  • 13
  • 21
26
votes
2 answers

PyYaml - Dump unicode with special characters ( i.e. accents )

I'm working with yaml files that have to be human readable and editable but that will also be edited from Python code. I'm using Python 2.7.3 The file needs to handle accents ( mostly to handle text in French ). Here is a sample of my issue: import…
Hans Baldzuhn
  • 317
  • 1
  • 3
  • 9
25
votes
3 answers

pyyaml and using quotes for strings only

I have the following YAML file: --- my_vars: my_env: "dev" my_count: 3 When I read it with PyYAML and dump it again, I get the following output: --- my_vars: my_env: dev my_count: 3 The code in question: with open(env_file) as f: …
Jeroen Jacobs
  • 1,423
  • 3
  • 16
  • 32
25
votes
3 answers

Formatting PyYAML dump() output

I have a list of dictionaries, which I want to serialize: list_of_dicts = [ { 'key_1': 'value_a', 'key_2': 'value_b'}, { 'key_1': 'value_c', 'key_2': 'value_d'}, ... { 'key_1': 'value_x',…
nope
  • 387
  • 1
  • 3
  • 8
24
votes
2 answers

Why fatal error: 'yaml.h' file not found when installing PyYAML?

I'm trying out downloading PyYAML and install it following the instructions here http://pyyaml.org/wiki/PyYAML So I downloaded the ZIP package: http://pyyaml.org/download/pyyaml/PyYAML-3.11.zip and then cd into that folder and run python setup.py…
Penny
  • 1,218
  • 1
  • 13
  • 32
24
votes
5 answers

How to update yaml file using python

I have a some.yaml file with the below contents. init_config: {} instances: - host: username: password: The yaml file should be parsed and updated as below. init_config: {} …
Chetan
  • 1,217
  • 2
  • 13
  • 27
1
2
3
55 56