Salvando dados Python em JSON com json
This commit is contained in:
24
aula117.json
Normal file
24
aula117.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"nome": "Luiz Otávio 2",
|
||||
"sobrenome": "Miranda",
|
||||
"enderecos": [
|
||||
{
|
||||
"rua": "R1",
|
||||
"numero": 32
|
||||
},
|
||||
{
|
||||
"rua": "R2",
|
||||
"numero": 55
|
||||
}
|
||||
],
|
||||
"altura": 1.8,
|
||||
"numeros_preferidos": [
|
||||
2,
|
||||
4,
|
||||
6,
|
||||
8,
|
||||
10
|
||||
],
|
||||
"dev": true,
|
||||
"nada": null
|
||||
}
|
||||
28
aula117.py
Normal file
28
aula117.py
Normal file
@@ -0,0 +1,28 @@
|
||||
import json
|
||||
|
||||
pessoa = {
|
||||
'nome': 'Luiz Otávio 2',
|
||||
'sobrenome': 'Miranda',
|
||||
'enderecos': [
|
||||
{'rua': 'R1', 'numero': 32},
|
||||
{'rua': 'R2', 'numero': 55},
|
||||
],
|
||||
'altura': 1.8,
|
||||
'numeros_preferidos': (2, 4, 6, 8, 10),
|
||||
'dev': True,
|
||||
'nada': None,
|
||||
}
|
||||
|
||||
with open('aula117.json', 'w', encoding='utf8') as arquivo:
|
||||
json.dump(
|
||||
pessoa,
|
||||
arquivo,
|
||||
ensure_ascii=False,
|
||||
indent=2,
|
||||
)
|
||||
|
||||
with open('aula117.json', 'r', encoding='utf8') as arquivo:
|
||||
pessoa = json.load(arquivo)
|
||||
# print(pessoa)
|
||||
# print(type(pessoa))
|
||||
print(pessoa['nome'])
|
||||
Reference in New Issue
Block a user