I am trying to use the Python Mid() function to return strings starting at the 5th character.
I have tried
!STR_NAME!.Mid(5:20)
but that gives me a traceback error
Can someone point me in the right direction?
Thanks
Never heard of the mid() functin and couldn't find it with dr. google.
myString <SPAN class="operator token">=</SPAN> <SPAN class="string token">'abcdefghijklmnopqrstuvwxyz'</SPAN> x <SPAN class="operator token">=</SPAN> myString<SPAN class="punctuation token">[</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">20</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#returns 'fghijklmnopqrst'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This works for me...
how do I use that expression on a Field in ArcGIS Pro so it returns values starting at the 5th character?
Slice lesson
<SPAN class="comment token"># ---- python slicing... </SPAN> b <SPAN class="operator token">=</SPAN> <SPAN class="string token">"abcdefghijklmnopqrstuvwxyz"</SPAN> str<SPAN class="punctuation token">(</SPAN>b<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">20</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="string token">'fghijklmnopqrst'</SPAN> str<SPAN class="punctuation token">(</SPAN>b<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="string token">'fghijklmnopqrstuvwxyz'</SPAN> str<SPAN class="punctuation token">(</SPAN>b<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="string token">'vwxyz'</SPAN> str<SPAN class="punctuation token">(</SPAN>b<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="string token">'abcde'</SPAN> <SPAN class="comment token"># ---- why use str????</SPAN> str<SPAN class="punctuation token">(</SPAN>None<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="string token">'No'</SPAN> None<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> Traceback <SPAN class="punctuation token">(</SPAN>most recent call last<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> File <SPAN class="string token">"<ipython-input-18-5a2d59b27828>"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> <SPAN class="operator token"><</SPAN>module<SPAN class="operator token">></SPAN> None<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> TypeError<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'NoneType'</SPAN> object <SPAN class="keyword token">is</SPAN> <SPAN class="operator token">not</SPAN> subscriptable<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
See Dan's suggestions below... or:
str<SPAN class="punctuation token">(</SPAN>!FieldName!<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">20</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
resources for you: Cutting and slicing strings in Python - Python Central , Python str() - Python Standard Library
as per the lesson... 'b' is anything
like str(!STR_NAME!)[5:20]
Thanks guys! I got it to execute but it erased more chars than I wanted. Now I just have to re run the models and change the parameters around when I get to this point.
Thanks for helping me guys!
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.