我有两本工作簿:
我试图将wkbk1中的单元格值与wkbk2中的值进行比较。
我遇到的问题是搜索会将wkbk1 IP = 10.10.10.16等同于wkbk2 IP = 10.10.10.168(以及任何其他变体)。
如果我搜索(wkbk1 IP + \ n),则无法比较单行单元格。
以下是我的代码:
#variable top store the highest row number
mRow = str(mapIP.get_highest_row())
eRow = str(assetSheet.get_highest_row())
i = 2 #variable for row number output, skips the first row (b/c it is the header row, duh)
#create data by comparing IP in map to IP in CMDB
for mapIpRow in mapIP['A1':'A' + mRow]:
for mapIpCell in mapIpRow:
for assetIpRow in assetSheet['E1':'E' + eRow]:
for assetIpCell in assetIpRow:
assetIp = str(assetIpCell.value)
mapIp = str(mapIpCell.value)
if mapIp in assetIp:
outSheet['A' + str(i)].value = mapIp
print(mapIp) #just for feedback that the program is running
dnsM = mapIP['B' + str(mapIpCell.row)].value
owner = assetSheet['F' + str(assetIpCell.row)].value
dnsQ = assetSheet['B' + str(assetIpCell.row)].value #cishort
dnsQ2 = assetSheet['C' + str(assetIpCell.row)].value #cialias
dnsQ3 = assetSheet['D' + str(assetIpCell.row)].value #ciDesc
ciIP = assetSheet['E' + str(assetIpCell.row)].value #ciIP
ciID = assetSheet['A' + str(assetIpCell.row)].value #ciID
outSheet['B' + str(i)].value = dnsM
outSheet['C' + str(i)].value = owner
outSheet['D' + str(i)].value = dnsQ
outSheet['E' + str(i)].value = dnsQ2
outSheet['F' + str(i)].value = dnsQ3
outSheet['G' + str(i)].value = ciIP
outSheet['H' + str(i)].value = ciID
print owner #just for feedback that the program is running
i = i + 1
else:
pass
答案 0 :(得分:0)
获取每个单元格值(可能包含0个,1个或多个IP)并使用.split('\n')
将其放入列表中。您的列表比较if mapIp in assetIp
仍应有效。
assetIp = assetIpCell.value.split('\n') if assetIpCell.value else ''
mapIp = str(mapIpCell.value)
if mapIp in assetIp:
这可以解决您的问题:
我遇到的问题是搜索会将wkbk1 IP = 10.10.10.16等同于wkbk2 IP = 10.10.10.168(以及任何其他变体)。
比较字符串时in
运算符将返回子字符串结果,即:
'a' in 'David' == True
'10.10.10.16` in `10.10.10.168` == True
in
运算符与列表一起使用时,只有在列表中存在完全匹配时才会返回True
;
'10.10.10.16`' in ['10.52.1.1', '99.32.9.0', '168.152.1.1', `10.10.10.168`] == False