mxutyuc
mxutyuc

Reputation: 11

Reading and writing YAML files via Python3

I'm writing a little python script (python 3.7) that should read and write YAML files. At the moment I'm at the point where I just want to read a given YAML file. I imported the YAML library and tried to read the file. The problem that I'm having at the moment is that the output is not nested as it is in the original file.

How can I write a YAML file in a structured way as shown in the section below.

My Code:

import yaml

with open("/Users/Ilumeyss/Desktop/05_PS_ITM/xDQ_Datenqualitaetsvereinbarung_1003222_2018-10-16_v22yaml.sec", 'r') as stream:
    try:
        print(yaml.safe_load(stream))
    except yaml.YAMLError as exc:
        print(exc)

This is what I'd like to have:

projectElements:
    company:
        supplier:
            dataSupplierInformation:
                name: "Textilfabrik GmbH & Co. KG"

                address: {strasse: "Industriestr 1", plz: "72555", ort: "Metzingen", land: "Germany"}

                xdqNr: 1001001
            dataUserInformation:
                name: "Handel24 Deutschland AG"
                address: {strasse: "Handelsweg 42", plz: "74078", ort: "Heilbronn", land: "Germany"}
                xdqNr: 1001002
        documentinformation:
            dokumentname: "Datenqualitaetsvereinbarung"
            xdqNr: 1003222

This is what I get so far:

{'projectElements': {'company': {'supplier': {'dataSupplierInformation': {'name': 'Textilfabrik GmbH & Co. KG', 'address':

...and so on

Upvotes: 0

Views: 1504

Answers (1)

Anthon
Anthon

Reputation: 76568

In order to print a YAML document as you have loaded it, you should dump the loaded data to stdout. I do recommend to use ruamel.yaml as it supports this kind of round-tripping much better than PyYAML (disclaimer: I am the author of ruamel.yaml). PyYAML only supports YAML 1.1 and that was outdated back in 2009.

If your input is in xDQ_Datenqualitaetsvereinbarung_1003222_2018-10-16_v22yaml.sec:

projectElements: 
  company:
    supplier:
      dataSupplierInformation:
        name: "Textilfabrik GmbH & Co. KG"

        address: {strasse: "Industriestr 1", plz: "72555", ort: "Metzingen", land: "Germany"}

        xdqNr: 1001001
      dataUserInformation:
        name: "Handel24 Deutschland AG"
        address: {strasse: "Handelsweg 42", plz: "74078", ort: "Heilbronn", land: "Germany"}
        xdqNr: 1001002
      documentinformation:
        dokumentname: "Datenqualitaetsvereinbarung"
        xdqNr: 1003222

And your program:

import sys
from pathlib import Path
import ruamel.yaml

infile = Path('xDQ_Datenqualitaetsvereinbarung_1003222_2018-10-16_v22yaml.sec')

yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
data = yaml.load(infile)
yaml.dump(data, sys.stdout)

your output will be:

projectElements:
  company:
    supplier:
      dataSupplierInformation:
        name: "Textilfabrik GmbH & Co. KG"

        address: {strasse: "Industriestr 1", plz: "72555", ort: "Metzingen", land: "Germany"}

        xdqNr: 1001001
      dataUserInformation:
        name: "Handel24 Deutschland AG"
        address: {strasse: "Handelsweg 42", plz: "74078", ort: "Heilbronn", land: "Germany"}
        xdqNr: 1001002
      documentinformation:
        dokumentname: "Datenqualitaetsvereinbarung"
        xdqNr: 1003222

and you cannot get that exact same output with PyYAML in any easy way.

Upvotes: 1

Related Questions