比较IronPython中的字符串
我试图在搜索要更新的WSUS组时比较两个字符串。然而,我的比较失败了,尽管它们在视觉上看起来是相同的,并且是相同的类型。由于这是IronPython,我在Komodo中没有可用的调试器(有人知道IP的调试器吗?) 不管怎样,有人能看出我做错了什么吗比较IronPython中的字符串,python,ironpython,Python,Ironpython,我试图在搜索要更新的WSUS组时比较两个字符串。然而,我的比较失败了,尽管它们在视觉上看起来是相同的,并且是相同的类型。由于这是IronPython,我在Komodo中没有可用的调试器(有人知道IP的调试器吗?) 不管怎样,有人能看出我做错了什么吗 #---------------------------------------------------------------------- # Search for a matching patch group, and approve th
#----------------------------------------------------------------------
# Search for a matching patch group, and approve them.
#----------------------------------------------------------------------
def WSUSApprove(apprvGrpName):
clr.AddReference('Microsoft.UpdateServices.Administration')
import Microsoft.UpdateServices.Administration
wsus = Microsoft.UpdateServices.Administration.AdminProxy.GetUpdateServer('wsus01',False,8530)
parentGroupCollection = wsus.GetComputerTargetGroups()
for computerTarget in parentGroupCollection:
if computerTarget.Name.ToString() == 'Servers':
parent = computerTarget
childGroupCollection = parent.GetChildTargetGroups()
for computerTarget in childGroupCollection:
print type(computerTarget.Name.ToString())
print type(apprvGrpName)
if apprvGrpName == computerTarget.Name.ToString():
print 'success', computerTarget.Name.ToString()
else:
print 'a', computerTarget.Name.ToString()
print 'b', apprvGrpName
#--output that should be equal--#
<type 'str'>
<type 'str'>
a 3 Tuesday
b 3 Tuesday
#----------------------------------------------------------------------
#搜索匹配的修补程序组,并批准它们。
#----------------------------------------------------------------------
def WSUSApprove(ApprovgpName):
clr.AddReference('Microsoft.UpdateServices.Administration')
导入Microsoft.UpdateServices.Administration
wsus=Microsoft.UpdateServices.Administration.AdminProxy.GetUpdateServer('wsus01',False,8530)
parentGroupCollection=wsus.GetComputerTargetGroups()
对于parentGroupCollection中的computerTarget:
如果computerTarget.Name.ToString()=“服务器”:
父对象=计算机目标
childGroupCollection=parent.GetChildTargetGroups()
对于childGroupCollection中的computerTarget:
打印类型(computerTarget.Name.ToString())
打印类型(ApprovgRPName)
如果ApprovgRPName==computerTarget.Name.ToString():
打印“成功”,computerTarget.Name.ToString()
其他:
打印“a”,computerTarget.Name.ToString()
打印“b”,approvgrpname
#--应该相等的输出--#
a星期二下午三点
b 3星期二
在Python2.x上,用于直观地查看两个字符串是否相同print
基本上调用str
,因此您无法看到无法打印的字符,也很难看到空格之间的差异
因此,请:
print repr(computerTarget.Name.ToString())
print repr(apprvGrpName)
找出它们不相等的原因
请参阅John Manchin的评论,了解在Python3.x上使用什么,其中
repr()
不会转义unicode字符。最有可能的一个字符串后面有一个空格字符,例如换行符、回车符或空格 其中一个上显示“\n”!!谢谢你的帮助。嗯,我会把这个问题转移到一个答案上来。请记住接受您的问题的答案(前两个问题您没有回答)。是的,他知道这一点,因为在我建议他回答后,他使用了repr
。请参阅关于该问题的注释。对于Python3.x,请使用ascii()
。Python3.xrepr()
不使用转义序列表示非ASCII字符,留下了相当大的歧义范围。不管这是Python1.x问题,不要说“总是”。