I have a list of dictionaries. Occasionally, I want to change and save one of these dictionaries so that the new message is utilized if the script is restarted. Right now, I make that change by modifying the script and rerunning it. I'd like to pull this out of the script and put the list of dictionaries into a configuration file of some kind.
我有一个词典列表。有时,我想更改并保存其中一个词典,以便在重新启动脚本时使用新消息。现在,我通过修改脚本并重新运行来进行更改。我想将其从脚本中拉出来并将字典列表放入某种配置文件中。
I've found answers on how to write a list to a file, but this assumes that it is a flat list. How can I do it with a list of dictionaries?
我已经找到了如何将列表写入文件的答案,但这假设它是一个平面列表。如何使用词典列表?
My list looks like this:
我的列表看起来像这样:
logic_steps = [
{
'pattern': "asdfghjkl",
'message': "This is not possible"
},
{
'pattern': "anotherpatterntomatch",
'message': "The parameter provided application is invalid"
},
{
'pattern': "athirdpatterntomatch",
'message': "Expected value for debugging"
},
]
4 个解决方案
#1
15
provided that the object only contains objects that JSON can handle (lists
, tuples
, strings
, dicts
, numbers
, None
, True
and False
), you can dump it as json.dump:
如果对象只包含JSON可以处理的对象(列表,元组,字符串,dicts,数字,None,True和False),则可以将其转储为json.dump:
import json
with open('outputfile', 'w') as fout:
json.dump(your_list_of_dict, fout)
#2
1
The way you will have to follow to write a dict to a file is kind different from the post you have mentioned.
将dict写入文件必须遵循的方式与您提到的帖子有所不同。
First, you need serialize the object and than you persist it. These are fancy names for "write python objects to a file".
首先,您需要序列化对象而不是持久化对象。这些是“将python对象写入文件”的奇特名称。
Python has 3 serialization modules included by default that you can use to achieve your objective. They are: pickle, shelve and json. Each one has its own characteristics and the one you have to use is the one which is more suitable to your project. You should check each module documentation to get more on it.
Python默认包含3个序列化模块,您可以使用它们来实现您的目标。他们是:泡菜,搁架和json。每个都有自己的特点,你必须使用的是更适合你的项目。您应该检查每个模块文档以获得更多信息。
If your data will be only be accessed by python code, you can use shelve, here is an example:
如果你的数据只能通过python代码访问,你可以使用shelve,这是一个例子:
import shelve
my_dict = {"foo":"bar"}
# file to be used
shelf = shelve.open("filename.shlf")
# serializing
shelf["my_dict"] = my_dict
shelf.close() # you must close the shelve file!!!
To retrieve the data you can do:
要检索数据,您可以执行以下操作:
import shelve
shelf = shelve.open("filename.shlf") # the same filename that you used before, please
my_dict = shelf["my_dict"]
shelf.close()
See that you can treat the shelve object almost the same way you do with a dict.
看到你可以像对待dict一样处理搁置对象。
#3
0
Just for completeness I add also the json.dumps()
method:
为了完整性,我还添加了json.dumps()方法:
with open('outputfile_2', 'w') as file:
file.write(json.dumps(logic_steps, indent=4))
Have a look here for the difference between json.dump()
and json.dumps()
看看json.dump()和json.dumps()之间的区别
#4
0
if you want each dictionary in one line:
如果你想在一行中的每个字典:
import json
output_file = open(dest_file, 'w', encoding='utf-8')
for dic in dic_list:
json.dump(dic, output_file)
output_file.write("\n")
#1
15
provided that the object only contains objects that JSON can handle (lists
, tuples
, strings
, dicts
, numbers
, None
, True
and False
), you can dump it as json.dump:
如果对象只包含JSON可以处理的对象(列表,元组,字符串,dicts,数字,None,True和False),则可以将其转储为json.dump:
import json
with open('outputfile', 'w') as fout:
json.dump(your_list_of_dict, fout)
#2
1
The way you will have to follow to write a dict to a file is kind different from the post you have mentioned.
将dict写入文件必须遵循的方式与您提到的帖子有所不同。
First, you need serialize the object and than you persist it. These are fancy names for "write python objects to a file".
首先,您需要序列化对象而不是持久化对象。这些是“将python对象写入文件”的奇特名称。
Python has 3 serialization modules included by default that you can use to achieve your objective. They are: pickle, shelve and json. Each one has its own characteristics and the one you have to use is the one which is more suitable to your project. You should check each module documentation to get more on it.
Python默认包含3个序列化模块,您可以使用它们来实现您的目标。他们是:泡菜,搁架和json。每个都有自己的特点,你必须使用的是更适合你的项目。您应该检查每个模块文档以获得更多信息。
If your data will be only be accessed by python code, you can use shelve, here is an example:
如果你的数据只能通过python代码访问,你可以使用shelve,这是一个例子:
import shelve
my_dict = {"foo":"bar"}
# file to be used
shelf = shelve.open("filename.shlf")
# serializing
shelf["my_dict"] = my_dict
shelf.close() # you must close the shelve file!!!
To retrieve the data you can do:
要检索数据,您可以执行以下操作:
import shelve
shelf = shelve.open("filename.shlf") # the same filename that you used before, please
my_dict = shelf["my_dict"]
shelf.close()
See that you can treat the shelve object almost the same way you do with a dict.
看到你可以像对待dict一样处理搁置对象。
#3
0
Just for completeness I add also the json.dumps()
method:
为了完整性,我还添加了json.dumps()方法:
with open('outputfile_2', 'w') as file:
file.write(json.dumps(logic_steps, indent=4))
Have a look here for the difference between json.dump()
and json.dumps()
看看json.dump()和json.dumps()之间的区别
#4
0
if you want each dictionary in one line:
如果你想在一行中的每个字典:
import json
output_file = open(dest_file, 'w', encoding='utf-8')
for dic in dic_list:
json.dump(dic, output_file)
output_file.write("\n")