내부 프로젝트의 일부로 DNS 영역 파일 레코드를 구문 분석해야합니다. 이 파일은 대략 이와 비슷합니다.구성에서 사전 사전을 만드는 Python
$ORIGIN 0001.test.domain.com.
test-qa CNAME test-qa.0001.test.domain.com.
$ORIGIN test-qa.domain.com.
unit-test01 A 192.168.0.2
$TTL 60 ; 1 minute
integration-test A 192.168.0.102
$ORIGIN dev.domain.com.
web A 192.168.10.10
$TTL 300; 5 minutes
api A 192.168.10.13
기본 TTL은 어디 언급 $의 TTL이 없기 때문에
test-qa CNAME test-qa.0001.test.domain.com.
3600의 TTL을 가지고, 위의 데이터, 즉, 3600이다. 그러나
unit-test01 A 192.168.0.2
는 3600 TTL과
integration-test A 192.168.0.102
60 초의 TTL을 가지고있다.
위의 데이터에서 데이터 구조를 만들려고합니다. 사전을 통해이 데이터를 트래버스 할 수있는 최선의 방법이라고 생각합니다.
내가 무슨 짓을 :
origin = re.compile("^\$ORIGIN.*")
ttl = re.compile("^$TTL.*")
default_ttl = "$TTL 3600"
data_dict = {}
primary_key = None
value = None
for line in data_zones:
if origin.search(line):
line = line.replace("$ORIGIN ", "")
primary_key = line
elif ttl.search(line):
default_ttl = line
else:
value = line
data_dict[primary_key] = [default_ttl]
data_dict[primary_key][default_ttl] = value
나는 사전에 변환 할,하지만 오류를
TypeError: list indices must be integers, not str
내 샘플 데이터 구조를 얻을
0001.test.domain.com.: #This would be the first level Key
ttl:3600: #This would be the second level key
test-qa CNAME test-qa.0001.test.domain.com. #Value
test-qa.domain.com.: #This would be the first level Key
ttl:3600: #This would be the second level key
unit-test01 A 192.168.0.2 #value
ttl:60: #This would be the second level key
integration-test A 192.168.0.102 #value
같은 것을 볼 필요가
여기서 내가 뭘 잘못하고 있니?