code:
func(!Sent Phone!)
pre-logic script:
<SPAN class="comment token"># only works for a string, add some</SPAN> <SPAN class="comment token">#logic to test for str, int float etc</SPAN> <SPAN class="comment token">#then if/else it in a similar block</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">func</SPAN><SPAN class="punctuation token">(</SPAN>input<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>input<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">8</SPAN><SPAN class="punctuation token">:</SPAN> result <SPAN class="operator token">=</SPAN> input <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> result <SPAN class="operator token">=</SPAN> <SPAN class="string token">'0'</SPAN> <SPAN class="keyword token">return</SPAN> result<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Ok so an integer is being passed into the function. Replace if len(input) with if len(str(input))
Executing (Calculate Field): CalculateField "C:\Users\Shp_salida\001.shp" Phone func( !Telefono_C! ) PYTHON_9.3 "def func(input):\n if len(input) == 8:\n result = input\n else:\n result = 0\n return result\n"Start Time: Thu May 21 15:59:34 2020ERROR 000539: Error running expression: func( 5 ) Traceback (most recent call last): File "<expression>", line 1, in <module> File "<string>", line 2, in funcTypeError: object of type 'int' has no len()
Failed to execute (Calculate Field).Failed at Thu May 21 15:59:34 2020 (Elapsed Time: 0.01 seconds)Failed to execute (Model72).Failed at Thu May 21 15:59:34 2020 (Elapsed Time: 1.34 seconds)
Sharing the full error message would be helpful
on looking back at my answer I'm also missing quotes to make 0 a string. Have edited or will try if able. Loving the one liner Joshua!
In addition to David's answer, this can be accomplished with a one-liner in the expression block that doesn't require a pre-calculated length field:
!Sent Phone! <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>!Sent Phone!<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">8</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="string token">'0'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
In your illustration, have you updated the Length field already, or is that to be part of the script?
My first thought is to use an update cursor with arcpy or do you want to keep this at the field calculator level?
R// I would like to keep it from the field calculator
Los miembros registrados pueden publicar, seguir actualizaciones y más. ¿Nuevo aquí? Regístrate gratis.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.