I have a field "height" and its in x" y' . I need to convert this to inches and then multiple by 2.54 to update "height_cm" field.
Nothing like a brain warmup before going to sleep;)
If you have any blanks or nulls, I don't check for them so only run this in the field calculator for valid heights
The result is a number, if you want a string replace line 7 with
return str((a + b) *2.54);<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
<SPAN class="comment token"># ---- python code block section</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">metric_is_better</SPAN><SPAN class="punctuation token">(</SPAN>val<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> r<SPAN class="string token">"""convert from 6' 2" to metric"""</SPAN> ft<SPAN class="punctuation token">,</SPAN> inch <SPAN class="operator token">=</SPAN> val<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" "</SPAN><SPAN class="punctuation token">)</SPAN> a <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN>ft<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="number token">12</SPAN> b <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN>inch<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">(</SPAN>a <SPAN class="operator token">+</SPAN> b<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN><SPAN class="number token">2.54</SPAN> <SPAN class="comment token"># expression</SPAN> metric_is_better<SPAN class="punctuation token">(</SPAN>!YourFieldName!<SPAN class="punctuation token">)</SPAN> <SPAN class="string token">""" Example... Yes... this is how you have to encode when you use ' and " in strings val = r"""</SPAN><SPAN class="number token">6</SPAN>' <SPAN class="number token">2</SPAN>\<SPAN class="string token">""</SPAN><SPAN class="string token">""</SPAN> val <SPAN class="string token">'6\' 2\\"'</SPAN> metric_is_better<SPAN class="punctuation token">(</SPAN>val<SPAN class="punctuation token">)</SPAN> <SPAN class="number token">187.96</SPAN> <SPAN class="comment token"># the result</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>
How would this work for a list of attributes (entire table)? Goal is to not alter the height column but to read the imperial units x'x" in column height and populate another column height_cm
Using Calculate Field
Calculate Field—Data Management toolbox | ArcGIS Desktop
with the Python parser and a code block
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.