2013-09-03 2 views
0

LDAP 쿼리가 있는데 파이썬에서 텍스트 처리에 정통하지 않습니다. 나는 stdin을 통해 스크립트에 보내고 그것을 읽을 수는 있지만 단 한 줄로 읽었을 때 나는 프로토콜에 대한 가치를 잡는 방법에 대해 좀 더 길을 잃었다. 주어진 protocol = HTTP, 나는 구분 기호 뒤에 값을 저장하고 싶다.Python의 stdin에서 LDAP 속성 가져 오기

내 표준 입력 보이는 같은 (그러나 정확히) :

discover-repository-location=null, File Name=null, date-detected=Tue Jun11 12:44:14 UTC 2013, endpoint-machine-name=null, incident-id=545527, sender-ip=12.1.141.87, sender-email=WinNT://tmpdm/tmpcmp, Assigned To=null, sender-port=-null, endpoint-domain-name=null, Business Unit=null, endpoint-dos-volume-name=null, file-access-date=null, date-sent=Tue Jun 11 12:44:14 UTC 2013, endpoint-file-name=null, file-modified-by=null, Country=null, Manager Email=null, plugin-chain-id=1, discover-server=null, data-owner-name=null, Dismissal Reason=null, Last Name=null, First Name=null, Phone=null, subject=HTTP incident, Sender Email=null, UserID=null, endpoint-user-name=null, endpoint-volume-name=null, discover-name=null, discover-content-root-path=null, data-owner-email=null, file-create-date=null, endpoint-application-name=null, Employee Code=null, Region=null, Manager First Name=null, path=null, endpoint-application-path=null, Manager Last Name=null, Department=null, discover-location=null, protocol=HTTP, Resolution=null, file-owner=null, Postal Code=null, endpoint-file-path=null, Title=null, discover-extraction-date=null, Script-attribute=null, Manager Phone=null, file-created-by=null, file-owner-domain=nul 

그리고 내가하여 찾을 수 있다는 것을 확신 할 수 있습니다 내 다음 단계에

for line in sys.stdin: 
    if 'protocol' in line: 
     print "Protocol found" 

어떤 아이디어 또는 포인터?

답변

1
line_dict 
for line in sys.stdin: 
    parts = line.split(",") 
    line_dict = dict(map(str.strip,part.split("=")) for part in parts) 
    print line_dict['protocol'] 

나는 약간의 구문 오류가있을 수 있지만 아마도 당신이 원하는 것이 무엇인지 알기 위해 테스트를하지 않았습니다. 그러나 방금 프로토콜을 원한다면

import re 
for line in sys.stdin: 
    if 'protocol' in line: 
     print re.findall("protocol\s*=([^,]*)",line) 
+0

우수. 나는 내 문제를 해결하기 위해 정규 표현식을 사용하는 것을 생각조차하지 않았다. 이는 텍스트를 분할하는 여러 for 루프를 단순화합니다. 감사! – Signus

+0

내가 가진 유일한 문제는 단일 변수보다는 목록을 만드는 것입니다. 위의 경우 '프로토콜'이 LDAP 쿼리에 두 번 이상 나열 될 것으로 예상되지만 그럴 수는 없습니다. 물론 잘 작동합니다. – Signus

+0

re.search를 쉽게 사용할 수 있지만 문자열 대신 일치하는 객체를 반환합니다 ... –