0

私は LDAP クエリを持っていますが、Python でのテキスト処理に精通していません。標準入力経由でスクリプトに送信していて、読み取ることができますが、1行として読み取られるため、プロトコルなどの値を取得する方法について少し迷っています。protocol=HTTP の場合、区切り文字の後に値を格納したいと考えています。

私のstdinは次のようになります(正確ではありません):

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"

次のステップに関するアイデアや指針はありますか?

4

1 に答える 1

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']

to be fair I didnt test it so there may be some minor syntax errors but something like that is probably what you want. however if you just wanted protocol

import re
for line in sys.stdin:
     if 'protocol' in line:
         print re.findall("protocol\s*=([^,]*)",line)
于 2013-09-03T16:21:02.743 に答える