【发布时间】:2015-10-21 04:47:25
【问题描述】:
我试图让我的 for 循环只打印一次请求,但它似乎打印了两次。如何防止这种情况发生?
import json
list_1 = ['name1','name2',]
list_2 = ['1.1.1.1','2.2.2.2',]
username = 'john'
password = 'test_password'
for x in list_1:
for y in list_2:
x = {'device_type': 'juniper', 'ip': y, 'username': username, 'password': password, 'port': 9822,'verbose': False,}
print (json.dumps(x, indent=1))
想要的结果
{
"device_type": "juniper",
"ip": "1.1.1.1",
"password": "test_password",
"port": 9822,
"verbose": false,
"username": "john"
}
{
"device_type": "juniper",
"ip": "2.2.2.2",
"password": "test_password",
"port": 9822,
"verbose": false,
"username": "john"
}
【问题讨论】:
标签: json python-3.x dictionary pretty-print