试了一下值为列表的字典存储读取用json等方式报错,还是ast好用
# 使用大括号 {} 来创建空字典
import ast
#创建一个字典变量
data = {}
A = "a"
B = ['B','1','2']
# # 添加第一个元素
data[A] = B
print(data)
#保存字典为txt
# open file for writing
f = open("dict.txt","w")
# write file
f.write( str(data) )
# close file
f.close()
#从txt文件中写txt
# reading the data from the file
with open('dict.txt') as f:
data = f.read()
print("Data type before reconstruction : ", type(data))
# reconstructing the data as a dictionary
d = ast.literal_eval(data)
print("Data type after reconstruction : ", type(d))
print(d['a'])
https://pythonspot.com/save-a-dictionary-to-a-file/
https://www.geeksforgeeks.org/how-to-read-dictionary-from-file-in-python/