這是 json {"name": ["Juan", "Alex"]}。我如何使用 python 在“名稱”中添加一些東西?提前感謝您的回復。這是我的代碼,他不作業:(
import json
enteredString = str(input())
json_file = 'list_of_workers.json'
data = json.load(open(json_file, "rb"))
data['name'].append(enteredString)
json.dump(data, open(json_file, "wb"))
enteredString = str(input())
json_file = 'list_of_workers.json'
data = json.load(open(json_file, "r"))
data['name'].append(enteredString)
json.dump(data, open(json_file, "w"))
(working_code)
uj5u.com熱心網友回復:
import json
enteredString = str(input())
json_file = 'list_of_workers.json'
data = json.load(open(json_file, "rb"))
data['name'].append(enteredString)
newdata = json.dumps(data, indent=4, sort_keys=True)
with open(json_file, 'w') as f:
f.write(newdata)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/394283.html
