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
12
votes
1 answer

In Django loaddata it throws errors for json format but work properly for yaml format, why?

In order to learn how to import initial data in database I created models as, from django.db import models class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) after that, I…
Arun Pal
  • 687
  • 7
  • 28
12
votes
1 answer

Why is PyYAML spending so much time in just parsing a YAML File?

I am parsing a YAML file with around 6500 lines with this format: foo1: bar1: blah: { name: "john", age: 123 } metadata: { whatever1: "whatever", whatever2: "whatever" } stuff: thing1: bluh1: { name: "Doe1", age: 123 } …
Pigueiras
  • 18,778
  • 10
  • 64
  • 87
11
votes
1 answer

how to keep null value in yaml file while dumping though ruamel.yaml

I have YAML file site.yaml: Kvm_BLOCK: ip_address: 10.X.X.X property: null server_type: zone loaded and then dumped with: ruamel.yaml.dump(site_yaml, new_file, Dumper=ruamel.yaml.RoundTripDumper) it becomes Kvm_BLOCK: ip_address:…
Satender346
  • 352
  • 1
  • 3
  • 15
11
votes
1 answer

How to create current local date and time with PyYAML

I'd like to be able to create a datetime object with datetime.datetime.now() PyYAML. It's easy to call some functions: >>> y = """#YAML ... description: Something ... ts: !!python/object/apply:time.time []""" >>> yaml.load(y) {'description':…
brianz
  • 7,268
  • 4
  • 37
  • 44
10
votes
1 answer

How do I write a representer for PyYAML?

I want to have a custom function that serializes arbitrary python objects, like how the json.dump function has an optional arg called 'default', that should be a function that the json dumper will call if the object is not json serializable. I…
whoisbenli
  • 1,095
  • 1
  • 9
  • 12
10
votes
4 answers

Changing a value in a yaml file using Python

I have a .yaml file I want to update with Python code. Let's say it looks something like that: state: 'present' I'd like to have a code that changes the state and saves the file. I'm trying with something like this and fail: def…
Pavel Zagalsky
  • 1,620
  • 7
  • 22
  • 52
10
votes
3 answers

yaml anchors definitions loading in PyYAML

I'm using PyYAML. Is there a way to define a YAML anchor in a way it won't be a part of the data structure loaded by yaml.load (I can remove "wifi_parm" from the dictionary but looking for a smarter way)? example.yaml: wifi_parm: &wifi_params …
adi
  • 253
  • 3
  • 8
10
votes
3 answers

pyyaml safe_load: how to ignore local tags

I am using yaml.safe_load() but I need to ignore a tag !v2 -- is there a way to do this but still use safe_load() ?
Jason S
  • 184,598
  • 164
  • 608
  • 970
10
votes
3 answers

How to deserialize an object with PyYAML using safe_load?

Having a snippet like this: import yaml class User(object): def __init__(self, name, surname): self.name= name self.surname= surname user = User('spam', 'eggs') serialized_user = yaml.dump(user) #Network deserialized_user =…
systempuntoout
  • 71,966
  • 47
  • 171
  • 241
9
votes
3 answers

Reading a configuration file in Python (storing/reading nested data with ConfigParser)

I am writing a list processing script that needs to read configuration data about each item in the list. The configuration data is best represented as a nested tree. I would normally have used YAML for storing the data - but I think that using…
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
9
votes
3 answers

How can I create a yaml file from pure python?

Example from Using YAML with Python Original YAML file contains this # tree format treeroot: branch1: name: Node 1 branch1-1: name: Node 1-1 branch2: name: Node 2 branch2-1: name: Node…
CppLearner
  • 16,273
  • 32
  • 108
  • 163
9
votes
2 answers

How to convert a pandas DataFrame to YAML in python

import yaml import pandas as pd data = ['apple','car','smash','market','washing'] bata = ['natural','artificail','intelligence','outstanding','brain'] df = pd.DataFrame(zip(data,bata),columns=['Data','Bata']) for columns in df: for list in…
Sarin Malcova
  • 93
  • 1
  • 3
9
votes
1 answer

What does the loader parameter do in yaml.load() function?

I am writing a code to read YAML file as input. I see following options as Loader in yaml.load call: BaseLoader, SafeLoader, FullLoader, UnsafeLoader. What do these options do? What does loading a full YAML language mean as mentioned in the…
Suzanno Hogwarts
  • 323
  • 2
  • 3
  • 12
9
votes
1 answer

python: how to add a new key and a value in yaml file

I have the following YAML file. I need to update the YAML file with a new key-value pair using python. I am doing the following but, it gives me error: pod = mylib.load_yaml("net/pod.yaml") pod['spec']['nodeSelector']['key']='val' it gives error…
Invictus
  • 2,653
  • 8
  • 31
  • 50
9
votes
2 answers

PyYaml "include file" and yaml aliases (anchors/references)

I had a large YAML file with a massive use of YAML anchors and references, for example: warehouse: obj1: &obj1 key1: 1 key2: 2 specific: spec1: <<: *obj1 spec2: <<: *obj1 key1: 10 The file got too large, so I looked for a…
benams
  • 4,308
  • 9
  • 32
  • 74