简体   繁体   中英

Comparing values in Excel cells

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 comparison if mapIp in assetIp should still work.

assetIp = assetIpCell.value.split('\n') if assetIpCell.value else ''
mapIp = str(mapIpCell.value)
if mapIp in assetIp:

This should resolve your problem:

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).

The in operator when comparing strings will return substring results, ie:

'a' in 'David' == True
'10.10.10.16` in `10.10.10.168` == True

The in operator when used with a list will return True only if an exact match exists in the list;

'10.10.10.16`' in ['10.52.1.1', '99.32.9.0', '168.152.1.1', `10.10.10.168`] == False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM