SearchCursor

276
13
07-09-2020 01:38 AM
KristýnaSeimlová
New Contributor

I try to find a match of numbers in the first column and second column, if it finds the same number in second column, it writes the value of the first column into third column. Do you have any ideas? 

0 Kudos
13 Replies
JoshuaSharp-Heward
Occasional Contributor

Hi Kristyna,

I've done something quite similar to this in the past! In this case you don't actually need to use cursors, just the field calculator using Python will do! If you right click on the third field (BELOW) and go to "calculate field", select "python" for the expression type, put the following code in the code block:

def myfunc(field1, field2):
if str(field1) in field2:
return field1
else:
return None

Then in the box above the codeblock, put the following line

my_func(!NADR_GID!, !ABOVE!)

It should do exactly what you're hoping for it to do. Basically taking the first number, converting it to a string, looking for that sequence in the second field, then returning the first field's value if there is a match!

0 Kudos
KristýnaSeimlová
New Contributor

Thanks! But there is one problem. Reason, why I tried to use SearchCursor is that I have more than one number in second culomn.

0 Kudos
DanPatterson
MVP Notable Contributor

!NADR_ID! if !NADR_ID! in !ABOVE! else None

try the 'in' operator


... sort of retired...
JoshuaSharp-Heward
Occasional Contributor

This shouldn't be an issue from my understanding, the multiple numbers in the second column are all just strings joined by ";"s, so if the number from the first column is anywhere in the second column it will return it. Have you tried running it? If so maybe you can show me where it has returned a value that you didn't want?

0 Kudos
KristýnaSeimlová
New Contributor

It returns no value. It returns a error 000989: Python syntax error.

0 Kudos
JoshuaSharp-Heward
Occasional Contributor

So in the original code I sent through, leave the field1 and field2 as they are (don't replace with your fields)! When you run myfunct(!NADR_GID!, !ABOVE!) that's substituting those values in there.

0 Kudos
KristýnaSeimlová
New Contributor

Unfortunately, it returns still same error.

0 Kudos
JoshuaSharp-Heward
Occasional Contributor

Can you post another picture of what you ran this time?

0 Kudos
KristýnaSeimlová
New Contributor

0 Kudos