In this example, how do I write a Python script to accomplish this result:
Site ID
SIte A 0001
Site A 0001
Site B 0002
SIte B 0002
Site C 0003
SIte D 0004
Prefix is always the same and suffix is always present and varies. Could I
substitute the data portion with the field name?
Charles Banks
Peeping in.....
A dictionary solution could look like something like this:
data <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'xyz_9999TUNK10_'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'xyz_9999TUNK10_'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'xyz_9999TUNK10_'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'xyz_9999BANK12_'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'xyz_9999BANK17_'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'xyz_9999BANK1_'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'xyz_9999BANK20_'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'xyz_9999BANK20_'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'xyz_9999BANK20_'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'xyz_9999BANK30_'</SPAN> <SPAN class="punctuation token">]</SPAN> d <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN> cnt <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> data<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> row <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> d<SPAN class="punctuation token">.</SPAN>keys<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> cnt <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN> d<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> cnt <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"{}{:04.0f}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">,</SPAN> cnt<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"{}{:04.0f}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">,</SPAN> d<SPAN class="punctuation token">[</SPAN>row<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="string token">''' Results: xyz_9999TUNK10_0001 xyz_9999TUNK10_0001 xyz_9999TUNK10_0001 xyz_9999BANK12_0002 xyz_9999BANK17_0003 xyz_9999BANK1_0004 xyz_9999BANK20_0005 xyz_9999BANK20_0005 xyz_9999BANK20_0005 xyz_9999BANK30_0006 '''</SPAN><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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Is the prefixed 'xyz_' always the same, or does it vary? If it varies, you will need to do some string splitting, etc.
Is the postfixed '_' always present? If not, the formatting statement can be modified to include it.
By making the dictionary 'd' and the counter 'cnt' global, you should be able to make it work for the field calculator. The code should also work with an update cursor, should you wish to go that route.
Sorry about the delay. Here it is:
show your field calculator dialog with the function and your expression filled in
Any of you other peeps want to leap in... lots of viewers... no suggestions from the gallery??? You can take over if you want
I got an error: 999999 : Error executing function.
<SPAN class="comment token"># lines 7... change to</SPAN> cnt <SPAN class="operator token">+=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="comment token"># ---- try changing this</SPAN> ret <SPAN class="operator token">=</SPAN> <SPAN class="string token">"{} {:04.0f}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>val<SPAN class="punctuation token">,</SPAN> cnt<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> cnt <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="comment token"># ---- and this</SPAN> ret <SPAN class="operator token">=</SPAN> <SPAN class="string token">"{} {:04.0f}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>val<SPAN class="punctuation token">,</SPAN> cnt<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Yes, I did, and got something like this:
did you substitute the !Test! name in the field calculator expression and try what I suggested?
perhaps you can take a screen grab of the field calculator dialog so we can see what you are doing
This is a sample of what needs to be:
It doesn't produce the exact sequence because... your data are different I suspect.
Perhaps you should show the data you are trying to produce a sequence for... or are you not following what the script does exactly?
This script does not produce the sequence above, it does work though!
The link is "
https://community.esri.com/thread/192672-sequential-numbering-python-script-for-field-calculator-in-arc-102".
I don't have Pro and I would have to do this from Field Calc.
Thanks
Charles
Well it can be done easily in more recent versions ( Table tools for ArcGIS PRO )
but perhaps if you included the link someone with an older version of ArcMap (or even 10.6) could have a look at what was posted there
This is in connection with post: "Sequential Numbering Python Script for
Field Calculator in Arc 10.2" but a different sequence in the data.
Thanks,
I am suspecting that there is more to it than that.
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.