I have two workbooks:
- wkbk1 has a single IP address in each cell of a specific column.
- wkbk2 has either no IP address, a single IP, or multiple IP's that are separated by a new line (vs a comma) in each cell of a specific column.
I am trying to compare the value of cell in wkbk1 to the values in wkbk2.
The problem I am having is that the search will compare a wkbk1 IP = 10.10.10.16 as equal to wkbk2 IP = 10.10.10.168 (and any other variant).
If I search by (wkbk1 IP + \n), it fails to compare single line cells.
The following is my code:
#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
Take each cell value (which may contain 0, 1, or multiple IPs) and put it into a list using
.split('\n')
. Your list comparisonif mapIp in assetIp
should still work.This should resolve your problem:
The
in
operator when comparing strings will return substring results, i.e.:The
in
operator when used with a list will returnTrue
only if an exact match exists in the list;