0
我在這裏有一組json對象。將json對象添加到未命名的json異議集
[
{
"group": "GroupName1",
"name": "Name1",
"nick": "Nick1",
"host": "Hostname1",
"user": "user1",
"sshport": "22",
"httpport": "80"
},
{
"group": "GroupName2",
"name": "Name2",
"nick": "Nick2",
"host": "hostname2",
"user": "user2",
"sshport": "22",
"httpport": "80"
}
]
我有一個CLI腳本服用的raw_input和構建包含該新的對象參數作爲這種新的dict對象:
def main():
# CLI Input
group_in = raw_input("Group: ")
name_in = raw_input("Name: ")
nick_in = raw_input("Nick: ")
host_in = raw_input("Host: ")
user_in = raw_input("User: ")
sshport_in = raw_input("SSH Port: ")
httpport_in = raw_input("HTTP Port: ")
# New server to add
jdict = {
"group": group_in,
"name": name_in,
"nick": nick_in,
"host": host_in,
"user": user_in,
"sshport": sshport_in,
"httpport": httpport_in
}
假設包含格式化爲例如上述的JSON對象JSON文件被加載爲:
with open(JSON_PATH, mode='r') as rf:
jf = json.load(rf)
我知道如何使用readlines方法/ writelines黑客的文件要做到這一點,但我將如何在年底增加 pythonically所以我可以只寫迴文件與完全新的對象以相同的方式格式化?
像往常一樣,我在思考過程中過度複雜。謝謝。 – oorahduc 2015-02-05 22:15:42