From 58896d5d9ac3357108719e1dcc4fc8ce9cea53be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Thu, 22 Dec 2022 09:15:15 -0300 Subject: [PATCH] json.dump e json.load com arquivos --- aula177.json | 15 +++++++++++++++ aula177.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 aula177.json create mode 100644 aula177.py diff --git a/aula177.json b/aula177.json new file mode 100644 index 0000000..78f310f --- /dev/null +++ b/aula177.json @@ -0,0 +1,15 @@ +{ + "title": "O Senhor dos Anéis: A Sociedade do Anel", + "original_title": "The Lord of the Rings: The Fellowship of the Ring", + "is_movie": true, + "imdb_rating": 8.8, + "year": 2001, + "characters": [ + "Frodo", + "Sam", + "Gandalf", + "Legolas", + "Boromir" + ], + "budget": null +} \ No newline at end of file diff --git a/aula177.py b/aula177.py new file mode 100644 index 0000000..7bd5458 --- /dev/null +++ b/aula177.py @@ -0,0 +1,29 @@ +# json.dump e json.load com arquivos +import json +import os + +NOME_ARQUIVO = 'aula177.json' +CAMINHO_ABSOLUTO_ARQUIVO = os.path.abspath( + os.path.join( + os.path.dirname(__file__), + NOME_ARQUIVO + ) +) + +filme = { + 'title': 'O Senhor dos Anéis: A Sociedade do Anel', + 'original_title': 'The Lord of the Rings: The Fellowship of the Ring', + + 'is_movie': True, + 'imdb_rating': 8.8, + 'year': 2001, + 'characters': ['Frodo', 'Sam', 'Gandalf', 'Legolas', 'Boromir'], + 'budget': None +} + +with open(CAMINHO_ABSOLUTO_ARQUIVO, 'w') as arquivo: + json.dump(filme, arquivo, ensure_ascii=False, indent=2) + +with open(CAMINHO_ABSOLUTO_ARQUIVO, 'r') as arquivo: + filme_do_json = json.load(arquivo) + print(filme_do_json)