cal*_*ion 4 python json dictionary
我有一个我正在阅读的json文件; 看起来类似于:
[
{
"Destination_IP": "8.8.4.4",
"ID": 0,
"Packet": 105277
},
{
"Destination_IP": "9.9.4.4",
"ID": 0,
"Packet": 105278
}
]
Run Code Online (Sandbox Code Playgroud)
当我解析json时:
for json_dict in data:
for key,value in json_dict.iteritems():
print("key: {0} | value: {0}".format(key, value))
Run Code Online (Sandbox Code Playgroud)
我正进入(状态:
key: Destination_IP | value: Destination_IP
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用.items(),我已经尝试过通过遍历键iterkeys()和keys()无济于事.
我可以直接调用它json_dict['Destination_IP']并返回值.
for json_dict in data:
if 'Destination_IP' in json_dict.keys():
print json_dict['Destination_IP']
Run Code Online (Sandbox Code Playgroud)
收益:
key: Destination_IP | value: 8.8.4.4
Run Code Online (Sandbox Code Playgroud)
我在python 2.7上,所以在运行值部分的任何帮助都将非常感激.
M.j*_*vid 14
更改字符串格式索引:
for json_dict in data:
for key,value in json_dict.iteritems():
print("key: {0} | value: {1}".format(key, value))
Run Code Online (Sandbox Code Playgroud)
或者不使用索引:
for json_dict in data:
for key,value in json_dict.iteritems():
print("key: {} | value: {}".format(key, value))
Run Code Online (Sandbox Code Playgroud)
您也可以使用名称而不是索引:
for json_dict in data:
for key,value in json_dict.iteritems():
print("key: {key} | value: {value}".format(key=key, value=value))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21539 次 |
| 最近记录: |