Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据用户输入更新JSON_Python_Json - Fatal编程技术网

Python 根据用户输入更新JSON

Python 根据用户输入更新JSON,python,json,Python,Json,我想基于用户的输入使用JSON创建数据库。我已经编写了这段代码,但是它用新数据替换了整个文件,并且没有更新现有的JSON文件 文件database.json的输出为 {"Employee ID": "ID2", "Employee Name": "Friendrich", "Domain": "Engineering", "Employee Type": &

我想基于用户的输入使用JSON创建数据库。我已经编写了这段代码,但是它用新数据替换了整个文件,并且没有更新现有的JSON文件

文件database.json的输出为

{"Employee ID": "ID2", "Employee Name": "Friendrich", "Domain": "Engineering", "Employee Type": "Permanent", "Start Date": "01.02.2020", "End Date": "28.02.2021"}
通过新的输入,json文件应该将数据添加到现有文件中

所以输出应该是

{"Employee ID": "new ID", "Employee Name": "input name", "Domain": "input domain", "Employee Type": "input type", "Start Date": "input date", "End Date": "input date"}, {"Employee ID": "ID2", "Employee Name": "Friendrich", "Domain": "Engineering", "Employee Type": "Permanent", "Start Date": "01.02.2020", "End Date": "28.02.2021"}, 

我创建此数据库是为了查找具有特定筛选器的所有员工。比如说所有从事领域工程的员工。使用JSON作为数据库是否是一种良好的做法?

每次保存数据时都会截断文件。
使用“a”而不是“w”作为打开模式

替换

with open('database.json', 'w') as json_file:
    json.dump(database, json_file)


每次保存数据时都会截断文件。
使用“a”而不是“w”作为打开模式

替换

with open('database.json', 'w') as json_file:
    json.dump(database, json_file)


关于您的问题,使用sqlite3(加上python)更新数据库更容易。看看

关于您的问题,使用sqlite3(加上python)更新数据库更容易。看看

如果您想要一个轻量级的数据库,而不是用JSON重新创建它,我会考虑使用
sqlite3
。谢谢如果您想要一个轻量级的数据库,而不是用JSON重新创建它,我会考虑使用
sqlite3
。谢谢
with open('database.json', 'a') as json_file:
    json.dump(database, json_file)