在IronPython中比较字符串
我正在尝试比较两个字符串,以便查找需要更新的WSUS组。不过,尽管这两个字符串在视觉上看起来是一样的,而且类型也相同,但我的比较却失败了。因为我在使用IronPython,所以在Komodo里没有调试工具(有人知道有没有适合IronPython的调试工具吗?)
总之,有人能帮我找出我哪里做错了吗?
#----------------------------------------------------------------------
# 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
2 个回答
0
很可能你的某个字符串后面有多余的空白字符,比如换行符、回车符或者空格。
1
在Python 2.x中,可以使用 repr()
来直观地查看两个字符串是否相同。因为 print
实际上是调用 str
,所以你看不到一些不可打印的字符,也很难发现空格的差异。
所以,你可以这样做:
print repr(computerTarget.Name.ToString())
print repr(apprvGrpName)
这样可以找出它们为什么不相等。
关于Python 3.x的使用方法,可以参考John Manchin的评论,因为在Python 3.x中,repr()
不会对unicode字符进行转义。