21.12.2022 Views

python_para_desenvolvedores_2ed

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

156 Persistência

}

print yaml.dump(progs)

Saída:

Alemanha:

Kraftwerk: [Radioactivity, Trans Europe Express]

Inglaterra:

Genesis: [Foxtrot, The Nursery Crime]

King Crimson: [Red, Discipline]

'Yes': [Close To The Edge, Fragile]

Exemplo de leitura de YAML. Arquivo de entrada “prefs.yaml”:

- musica: rock

- cachorro:

cor: Branco

nome: Bandit

raca: Labrador

- outros:

instrumento: baixo

linguagem: [python, ruby]

comida: carne

Código em Python:

import pprint

import yaml

# yaml.load() pode receber um arquivo aberto

# como argumento

yml = yaml.load(file('prefs.yaml'))

# pprint.pprint() mostra a estrutura de dados

# de uma forma mais organizada do que

# o print convencional

pprint.pprint(yml)

Saída:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!