'How to append python dictionary to text file

I tried the following the code

import json
final_key = ["letters", "words", "score"]
final_list = []
letters_1=['U', 'I', 'J', 'T', 'D', 'F', 'S', 'H', 'J']
final_list.append(letters_1)
word=['U', 'T', 'S']
final_list.append(word)
score = 3
final_list.append(score)
res = {}
for key in final_key:
    for value in final_list:
        res[key] = value
        final_list.remove(value)
        break
with open('log.txt', 'w') as convert_file:
     convert_file.write(json.dumps(res))

Then I got the following output

{"letters": ["U", "I", "J", "T", "D", "F", "S", "H", "J"], "words": ["U", "T", "S"], "score": 3}

After that creation, I need to append another dictionary to the above-created text. Ro achieve that I tried following code

final_list_1=[]
letters_2=['A', 'P', 'J', 'P', 'F', 'F', 'L', 'H', 'P']
final_list_1.append(letters_2)
word_1=['L', 'V', 'S','G']
final_list_1.append(word_1)
score_1 = 10
final_list_1.append(score_1)
res_1 = {}
for key in final_key:
    for value in final_list_1:
        res_1[key] = value
        final_list_1.remove(value)
        break
with open('log.txt', 'a') as convert_file:
     convert_file.write(json.dumps(res_1)) 

My output was

{"letters": ["U", "I", "J", "T", "D", "F", "S", "H", "J"], "words": ["U", "T", "S"], "score": 3}
{"letters": ["A", "P", "J", "P", "F", "F", "L", "H", "P"], "words": ["L", "V", "S", "G"], "score": 10}

But I need the following kind of output

[
{"letters": ["U", "I", "J", "T", "D", "F", "S", "H", "J"], "words": ["U", "T", "S"], "score": 3},

{"letters": ["A", "P", "J", "P", "F", "F", "L", "H", "P"], "words": ["L", "V", "S", "G"], "score": 10}
]

What kind of change does the code have to make for this?



Solution 1:[1]

One way is to write the list of the dictionary elements to the file at the end

with open('log.txt', 'w') as convert_file:
    conver_file.write(json.dumps([res, res_1]))

Edit: As OP wants to append dictionary elements every time its created, the steps are as follows:

At the beginning of the code, insert an empty list into the file

with open('log.txt', 'w') as convert_file:
    convert_file.write(json.dumps([]))

While appending a dictionary, remove the last square bracket, append the dictionary and then add the bracket again

with open('log.txt', 'rb+') as convert_file:
    convert_file.seek(-1, 2)
    convert_file.truncate()

with open('log.txt', 'a') as convert_file:
    convert_file.write(json.dumps(res))
    convert_file.write(']')

Using rb+ because it opens the file in binary format and also allows to edit the contents in the file

Solution 2:[2]

Actually you already got the solution. Just need to add json.loads(lines[0]) in the final line to get rid of the escape character \ :

with open('log.txt', 'w') as convert_file:
    convert_file.write(json.dumps([json.loads(lines[0]), res_1]))

Edit: Here's the full solution (mostly from OP):

import json
final_key = ["letters", "words", "score"]
final_list = []
letters_1=['U', 'I', 'J', 'T', 'D', 'F', 'S', 'H', 'J']
final_list.append(letters_1)
word=['U', 'T', 'S']
final_list.append(word)
score = 3
final_list.append(score)
res = {}
for key in final_key:
    for value in final_list:
        res[key] = value
        final_list.remove(value)
        break
with open('log.txt', 'w') as convert_file:
     convert_file.write(json.dumps(res))

final_list_1=[]
letters_2=['A', 'P', 'J', 'P', 'F', 'F', 'L', 'H', 'P']
final_list_1.append(letters_2)
word_1=['L', 'V', 'S','G']
final_list_1.append(word_1)
score_1 = 10
final_list_1.append(score_1)
res_1 = {}
for key in final_key:
    for value in final_list_1:
        res_1[key] = value
        final_list_1.remove(value)
        break
with open('log.txt') as f:
    lines = f.readlines()
with open('log.txt', 'w') as convert_file:
    convert_file.write(json.dumps([json.loads(lines[0]), res_1]))

Output log.txt

[
{"letters": ["U", "I", "J", "T", "D", "F", "S", "H", "J"], "words": ["U", "T", "S"], "score": 3}, 
{"letters": ["A", "P", "J", "P", "F", "F", "L", "H", "P"], "words": ["L", "V", "S", "G"], "score": 10}
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2