I need help with writing VBA or python script that will merge fields in way I did it in excel (see formula applied on red column in attached spreadsheet).
Basically merging fields but with some conditions implemented.
My guess, because I can't load the spreadsheet because of the embedded vba and just going by your screen grab of the perfect output. Here are 2 examples
<SPAN class="string token">"{:>04.0f}{:>03.0f}{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="number token">11</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'z'</SPAN><SPAN class="punctuation token">.</SPAN>lower<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="string token">'0011002z'</SPAN> <SPAN class="string token">"{:>04.0f}{:>03.0f}{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1009</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">17</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'A'</SPAN><SPAN class="punctuation token">.</SPAN>lower<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="string token">'1009017a'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Which would suggest that your field calculator expression using a python parser with fields named
!A!, !B!, !C! would be
<SPAN class="string token">"{:>04.0f}{:>03.0f}{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>!A!<SPAN class="punctuation token">,</SPAN> !B!<SPAN class="punctuation token">,</SPAN> !C!<SPAN class="punctuation token">.</SPAN>lower<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
This translates to
{:>04.0f} means 4 character spaces with 0 padding, hence if the width of the number is 2, you get 2 padded 0's
{:>03.0f} same as above, but a 3 character space with 0 padding.
{} means, just a string representation
The ! marks are thrown around the field name if using a python parser
So the 2nd example block is the field calculator expression when you replace A, B, C with the actual field names, and you are calculating into a new text field
Thanks on replies everyone..
I am novice to any type of scripting so I don't get it what to type in field calculator..
I need script which will populate new field, make concatenation of three fields (GJ, Odeljenje, Odsek) with couple conditions in field Odeljenje (test length of the content) and in field Odsek (test is the content number and length of it and if it is not change the case of text),
basically translate excel formula
CONCATENATE(A2,IF(LEN(B2)=1,CONCATENATE("00",B2),IF(LEN(B2)=2,CONCATENATE("0",B2),B2)),IF(ISNUMBER(C2+1),IF(LEN(C2)<2,CONCATENATE("0",C2),C2),LOWER(C2)))
to script (either with python or vba).
Try the following expression using the Python parser:
<SPAN class="string token">"{}0{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>!odeljenje!<SPAN class="punctuation token">,</SPAN>!odesk!<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" "</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">""</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
security toasted your vba macro I presume. So just in case you needed to check for nulls, here is the principle behind it. It will produce a concatenated text string for three fields
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">conc</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> b<SPAN class="punctuation token">,</SPAN> c<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> vals <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>i <SPAN class="keyword token">if</SPAN> i <SPAN class="keyword token">else</SPAN> i <SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">[</SPAN>a<SPAN class="punctuation token">,</SPAN> b<SPAN class="punctuation token">,</SPAN> c<SPAN class="punctuation token">]</SPAN> <SPAN class="punctuation token">]</SPAN> txt <SPAN class="operator token">=</SPAN> <SPAN class="string token">"{}{}{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> b<SPAN class="punctuation token">,</SPAN> c<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> txt conc<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1000</SPAN><SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'a'</SPAN><SPAN class="punctuation token">)</SPAN> Out<SPAN class="punctuation token">[</SPAN><SPAN class="number token">19</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'1000Nonea'</SPAN> conc<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1000</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'a'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN> Out<SPAN class="punctuation token">[</SPAN><SPAN class="number token">20</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'1000a5'</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>
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.