json.dump e json.load com arquivos

This commit is contained in:
Luiz Otávio
2022-12-22 09:15:15 -03:00
parent fcccbbd6d1
commit 58896d5d9a
2 changed files with 44 additions and 0 deletions

15
aula177.json Normal file
View File

@@ -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
}

29
aula177.py Normal file
View File

@@ -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)