python json匹配多个并替换

时间:2016-05-16 14:01:01

标签: python json

需要您的帮助才能找到解决方案。我有以下json

[{ “源”: “AA”, “目标”: “BB”, “指标”: “10”, “local_interface”: “100”, “remote_interface”: “200”}, { “源”: “AA”, “目标”: “BB”, “指标”: “10”, “local_interface”: “111”, “remote_interface”: “222”}, { “源”: “BB”, “目标”: “AA”, “指标”: “10”, “local_interface”: “200”, “remote_interface”: “100”}]

目标是:

  • 获取第一个元素(即:pattern)作为source / target / local_interface
  • 搜索匹配source = pattern.target,target = pattern.source,remote_interface = pattern.local_interface
  • 的剩余元素
  • 从json中删除元素。

希望这是有道理的。

1 个答案:

答案 0 :(得分:0)

你是在追求像这样简单的事情吗?

j = [{"source":"AA","target":"BB","metric":"10",
 "local_interface":"100","remote_interface":"200"},
 {"source":"AA","target":"BB","metric":"10",
 "local_interface":"111","remote_interface":"222"},
 {"source":"BB","target":"AA","metric":"10",
 "local_interface":"200","remote_interface":"100"}]

s = j[0]["source"]
print(s)
t = j[0]["target"]
print(t)
li = j[0]["local_interface"]
print(li)

print('Find source ==', t)
for i in range(1,3):
  if j[i]['source'] == t:
    print("Is match:", j[i])

print()

del(j[1])
print("after deletion:", j)