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?
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:
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">myfunc</SPAN><SPAN class="punctuation token">(</SPAN>field1<SPAN class="punctuation token">,</SPAN> field2<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> str<SPAN class="punctuation token">(</SPAN>field1<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">in</SPAN> field2<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> field1 <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> None<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Then in the box above the codeblock, put the following line
my_func<SPAN class="punctuation token">(</SPAN>!NADR_GID!<SPAN class="punctuation token">,</SPAN> !ABOVE!<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
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!
Why do you need a search cursor? you can use the field calculator for that
python parser
!NADR_ID! if !NADR_ID! == !ABOVE! else None
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!
サインインしたメンバーは投稿、更新のフォローなどができます。初めてですか?無料アカウントを登録してください。
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.