Python - 通过多次调用函数来保存值
我多次调用Python脚本,我想保存字典的值,但每次调用时它都会被初始化。我已经在
def__init__(self):
self.newdictt = dict()
这是我正在使用的代码片段中定义了该变量:
my_dict = {"camel": config_file, service: thisisjson_dict}
self.newdictt.update(my_dict)
with open('data.json', 'w') as json_file:
json.dump(self.newdictt, json_file)
但是 self.newdictt 每次都会被覆盖
I call a Python script multiple times and I want to save value of a dictionary, but it gets initialized every time when it is called. I've defined that variable in
def__init__(self):
self.newdictt = dict()
This is the piece of code I'm using:
my_dict = {"camel": config_file, service: thisisjson_dict}
self.newdictt.update(my_dict)
with open('data.json', 'w') as json_file:
json.dump(self.newdictt, json_file)
But self.newdictt gets overwrited every time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经将字典保存到文件中。在创建空字典之前,首先检查文件是否存在并读取它。
You are already saving the dict to a file. Before you create an empty dict, first check if the file exists and read it.