0

我有一个 LDAP 查询,但我并不精通 Python 中的文本处理。我通过标准输入将它发送到我的脚本中,我可以把它读出来,但是考虑到它是单行的,我对如何获取协议的值有点迷茫。给定协议 = HTTP,我想将值存储在分隔符之后。

我的标准输入看起来像(但不完全是):

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

公平地说,我没有测试它,所以可能会有一些小的语法错误,但类似的东西可能是你想要的。但是,如果您只想要协议

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