【发布时间】:2014-10-21 19:54:39
【问题描述】:
我有以下脚本,但无法让它搜索字符串然后输出值。
如何获取参数并将其拆分,以便搜索“endpoint-machine-name=”然后输出其值?
命令如下:
python -u HostnameScript.py "discover-repository-location=null, Employee Notified=null, Manager Title=Exec Dir Biostatistics, date-detected=Mon Aug 25 16:03:35 PDT 2014, endpoint-machine-name=Davidpc, incident-id=603, sender-ip=null, Machine Name=null, Assigned To=null, Business Unit=Development US"
我尝试过拆分,但无法正确搜索。
import sys, socket, string, commands, os, re, subprocess
arguments=sys.argv[1:]
for args in [item.split(", ") for item in arguments[]:
if item.find("endpoint-machine-name=") != -1
value=item.strip("endpoint-machine-name=")
sys.stdout.write('Hostname=');print value
我最终得到的是
Hostname=discover-repository-location=null, Employee Notified=null, Manager Title=Exec Dir Biostatistics, date-detected=Mon Aug 25 16:03:35 PDT 2014, endpoint-machine-name=Davidpc, incident-id=603, sender-ip=null, Machine Name=null, Assigned To=null, Business Unit=Development US
【问题讨论】:
-
缩进一个空格并用分号将不相关的语句放在一行中会使您的代码难以阅读。此外,您的代码中至少有两个
SyntaxErrors 和一个IndentationError;你能贴一个running, minimal example吗? -
同时,您的代码中有一个非常明显的错误:您正在搜索
"endpoint-machin-name=",但您的数据有"endpoint-machine-name=",这不是同一个字符串。 -
argument[]是什么? -
无论如何,您的错误似乎是(1)您在搜索字符串中的拼写错误意味着没有匹配项,(2)您使用的是
item而不是args应该是拆分每个item的结果,并且(3)您的sys.stdout.write可能缩进错误,因此它在if之外。作为这三个错误的组合,对于每个项目,无论如何,您都会打印整个项目,前面有Hostname=,它描述了您的输出。但是没有看到实际代码,我无法告诉你如何修复它。 -
抱歉所有这些都是我的错别字。
标签: python search find arguments