1
I have a function that saves a Dict in a file, but this is simultaneous, I mean, every time I have a new Dict. I couldn’t apply the concept of append to this problem.
The Archive is being saved this way:
{
"num_conta": "11_OPEN.json",
"item_num": "333",
"item_desc": "COCA 3",
"item_price": "5.00"
}
{
"num_conta": "11_OPEN.json",
"item_num": "222",
"item_desc": "FRANGO PEDACO",
"item_price": "5.00"
}
I wish it was a list of Icts, that way:
[
{
"num_conta": "11_OPEN.json",
"item_num": "333",
"item_desc": "COCA 3",
"item_price": "5.00"
},
{
"num_conta": "11_OPEN.json",
"item_num": "222",
"item_desc": "FRANGO PEDACO",
"item_price": "5.00"
}
]
My Class
class Conta:
def __init__(self, num_conta, item_num, item_desc, item_price):
self.num_conta = num_conta
self.item_num = item_num
self.item_desc = item_desc
self.item_price = item_price
And here’s my job to save the file:
if not os.path.exists('data/today/CHECK/' + conta.num_conta):
with open('data/today/CHECK/' + conta.num_conta, 'w') as arquivo:
json.dump(conta.__dict__, arquivo, indent=4)
arquivo.close()
else:
arquivo = open('data/today/CHECK/' + conta.num_conta, 'a')
# json_string = json.dumps(data_request)
json.dump(conta.__dict__, arquivo, indent=4)
arquivo.close()
Any help is good life, thank you.
Hi Lucas, thank you so much for the answer, I managed to solve in a simpler way but I do not know if it is the most platonic way. I’m posting the code with the explanation.
– Rodolfo Sousa