如何添加python字典到文本文件



我尝试了以下代码

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

然后得到以下输出

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

创建之后,我需要向上面创建的文本追加另一个字典。为了达到这个目的,我尝试了以下代码

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

输出为

{"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}

但是我需要以下类型的输出

[
{"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}
]

代码需要做什么样的改变?

一种方法是将字典元素列表写入文件的末尾

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

编辑由于OP希望在每次创建时附加字典元素,因此步骤如下:

在代码的开头,向文件

插入一个空列表
with open('log.txt', 'w') as convert_file:
convert_file.write(json.dumps([]))

在追加字典时,去掉最后一个方括号,追加字典,然后再加上方括号

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(']')

使用rb+,因为它以二进制格式打开文件,并且还允许编辑文件

中的内容

其实你已经有答案了。只需要在最后一行添加json.loads(lines[0])以摆脱转义字符:

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

编辑:这是完整的解决方案(主要来自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]))

输出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}
]

最新更新