아래 코드의 PART II에 문제가 있습니다. 파일에 "Veth"
, "Po"
, "Eth"
에 대한 중복 항목이 있습니다.Dict에 가치 목록을 추가 할 때 문제가 있습니까?
23 0050.5681.6e60 dynamic 10 F F Veth1300
23 0050.5681.6e60 dynamic 10 F F Veth1400
23 0050.5681.6e60 dynamic 10 F F Veth1500
내 모든 생각은 MAC과 값과리스트로 인터페이스 ("Veth1300"
를) 고유 키와 일치로 MAC ("0050.5681.6e60"
)를 사용하여 캡처하는 것입니다 : 같은. 뭔가 같은 : 어떤 이유
0050.5681.6e60 : [Veth1300, Veth1400, Veth1500]
파트 II에 내 코드는 목표를 달성하지 않습니다. 파트 1의 My Regex 부분이 원하는대로 작동합니다. 이 오류가 계속 발생합니다.
Traceback (most recent call last):
File "C:\Python27\Mastin_Veth.py", line 24, in <module>
if dict1[mac] != None:
KeyError: '0050.5681.6e60'
누군가가 문제 진단에 도움을 줄 수 있습니까?
import re
MAC_keys = set()
dict1 = dict()
#Part I
with open('C:/Python27/dup_MAC_SR629930635.csv') as f:
for line in f:
regex = re.compile(r"\*\s*[0-9]{1,}\s*([0-9a-z]{4}.[0-9a-z]{4}.[0-9a-z]{4}).*(Veth\d+|Eth.*|Po\d+)$",re.IGNORECASE|re.DOTALL)
r = regex.search(line)
MAC_Key,MAC_associated_VETH = r.groups()
MAC_keys.add(MAC_Key)
#Part II
for mac in MAC_keys: # Iteration over SET of Keys
with open('C:/Python27/dup_MAC_SR629930635.csv') as f:
for line in f:
regex = re.compile(r"\*\s*[0-9]{1,}\s*([0-9a-z]{4}.[0-9a-z]{4}.[0-9a-z]{4}).*(Veth\d+|Eth.*|Po\d+)$",re.IGNORECASE|re.DOTALL)
r = regex.search(line)
MAC_Key,MAC_associated_VETH = r.groups()
if mac == MAC_Key:
#get the value associated with the key
if dict1[mac] != None:
tmp_values_list = list(dict1[mac])
tmp_values_list.append(MAC_associated_VETH)
dict1.update({mac:tmp_values_list })
elif dict1[mac] == None:
tmp_values_list = [MAC_associated_VETH]
dict1[mac] = tmp_values_list