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?
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
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!
Thanks! But there is one problem. Reason, why I tried to use SearchCursor is that I have more than one number in second culomn.
!NADR_ID! if !NADR_ID! in !ABOVE! else None
try the 'in' operator
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?
It returns no value. It returns a error 000989: Python syntax error.
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.
Unfortunately, it returns still same error.
Can you post another picture of what you ran this time?
That really should run fine, I don't see how that's giving a syntax error, unless it doesn't like the indentation. In any case I defer to Dan's solution which is running the same thing but in one line:
It doesn't work for me, but thank you so much for your help anyway.
I realise that should be "NADR_GID" not "NADR_ID", where are you putting that line of code? It should be in the box below the code block, and the code block should be empty! If that fails, I'd be happy to take another look at the error message it gives.
Also I realised in your previous screenshot it's running as Python9.3, try the above line or my original solution with "Python" as the expression type just in case!
Why do you need a search cursor? you can use the field calculator for that
!NADR_ID! if !NADR_ID! == !ABOVE! else None
Retrieving data ...