<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Field Calculator - Diameter conversion with conditions in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446076#M35025</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Love to move to Pro but it does not accept MS Access which is critical to our operations.&amp;nbsp; The choice of DB's is not mine to make!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 12 Dec 2017 15:04:33 GMT</pubDate>
    <dc:creator>TedKowal</dc:creator>
    <dc:date>2017-12-12T15:04:33Z</dc:date>
    <item>
      <title>Field Calculator - Diameter conversion with conditions</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446068#M35017</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;I am very new with python so have been really struggling with the field calculator for what seems like a simple task. (it is in excel anyway)&lt;BR /&gt;&lt;BR /&gt;I have a shapefile with a column named d_diameter, but this column is String and contains entires; 100mm, 2in,4in, 70in, 50mm, 73mm etc. I am looking to convert these to numbers, specifically&amp;nbsp;to milimeters (mm) and to 2 decimal points. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So ones with "mm" extension, I just want the number. Entries with the "in" extension&amp;nbsp;in I want this removed and&amp;nbsp;the value timesed&amp;nbsp;(*) by 25.4 to convert it to mm.&lt;/P&gt;&lt;P&gt;e.g. 100mm becomes 100 and 2in becomes 50.8 etc&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The kicker is sometimes it has NULL values, or Text "Other" or "Unknown". Ideally I want in these cases for it to come through as NULL.&lt;BR /&gt;&lt;BR /&gt;Any idea or input where to start would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thomas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 Dec 2017 13:30:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446068#M35017</guid>
      <dc:creator>THomasDanks</dc:creator>
      <dc:date>2017-12-11T13:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator - Diameter conversion with conditions</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446069#M35018</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Post a sample of how the data is formatted; sounds like the field is of text type and you need the strip off the mm?&amp;nbsp; Take a look at&amp;nbsp;&lt;A class="link-titled" href="http://python-reference.readthedocs.io/en/latest/docs/str/rstrip.html" title="http://python-reference.readthedocs.io/en/latest/docs/str/rstrip.html"&gt;rstrip — Python Reference (The Right Way) 0.1 documentation&lt;/A&gt;&amp;nbsp;; rstrip is your friend.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then take a look at the int() function:&amp;nbsp;&lt;A class="link-titled" href="http://python-reference.readthedocs.io/en/latest/docs/ints/index.html?highlight=int()" title="http://python-reference.readthedocs.io/en/latest/docs/ints/index.html?highlight=int()"&gt;int — Python Reference (The Right Way) 0.1 documentation&lt;/A&gt;&amp;nbsp; Once you&amp;nbsp; get rid of the mm, you can treat a string like a number.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Finally, you can use round() to the decimal place of your choice:&amp;nbsp;&lt;A class="link-titled" href="http://python-reference.readthedocs.io/en/latest/docs/functions/round.html" title="http://python-reference.readthedocs.io/en/latest/docs/functions/round.html"&gt;round — Python Reference (The Right Way) 0.1 documentation&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Untested and probably just one of a myriad of ways to approach it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 Dec 2017 15:04:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446069#M35018</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2017-12-11T15:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator - Diameter conversion with conditions</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446070#M35019</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for the reply. I tried a similar thing with remove !d_material!.replace("in",("").replace("mm",""). I became stuck though because I only need to multiply the inches e.g. 2in, 4in, 15in by 25.4 and leave the milimeter (mm) ones alone in that caluclation. I didn't know how to do that though. Seems like I need a else if statement, but not sure how to contruct one.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The int function is very good to know thanks. A sample of my data with the column datatype is shown below:&lt;IMG alt="" class="image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/390429_Capture2.PNG" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 Dec 2017 15:18:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446070#M35019</guid>
      <dc:creator>THomasDanks</dc:creator>
      <dc:date>2017-12-11T15:18:29Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator - Diameter conversion with conditions</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446071#M35020</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;try this but your field calculator expression would be&lt;/P&gt;&lt;P&gt;cal(!YourFieldWithTheOriginalDataHere!&amp;nbsp;&lt;/P&gt;&lt;P&gt;using the python parser&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;cal&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'in'&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; a&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;return&lt;/SPAN&gt; str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;float&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;split&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'in'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="operator token"&gt;*&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;2.54&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'mm'&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;else&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;return&lt;/SPAN&gt; a

a &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'9in'&lt;/SPAN&gt;
b &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'22.86mm'&lt;/SPAN&gt;

cal&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;a&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&amp;nbsp; &lt;SPAN class="comment token"&gt;#'22.86mm'&lt;/SPAN&gt;
cal&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;b&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&amp;nbsp; &lt;SPAN class="comment token"&gt;#'22.86mm'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:56:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446071#M35020</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T19:56:26Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator - Diameter conversion with conditions</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446072#M35021</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Pre-Logic Scipt Code:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;def&lt;/SPAN&gt; dmm &lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;dia&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
 &lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; dia &lt;SPAN class="keyword token"&gt;is&lt;/SPAN&gt; &lt;SPAN class="operator token"&gt;not&lt;/SPAN&gt; None&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
 units&lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt;dia&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;2&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;
 &lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; units&lt;SPAN class="operator token"&gt;==&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'in'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
 cnv &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; int&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;int&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;filter&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;str&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;isdigit&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;dia&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="operator token"&gt;*&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;25.4&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
 &lt;SPAN class="keyword token"&gt;return&lt;/SPAN&gt; str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;cnv&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="operator token"&gt;+&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'mm'&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;YourField=&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;dmm&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt; !d_diameter!&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:56:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446072#M35021</guid>
      <dc:creator>TedKowal</dc:creator>
      <dc:date>2021-12-11T19:56:29Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator - Diameter conversion with conditions</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446073#M35022</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dan's is better --- the sample code I was using had unicode strings in it ....&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 Dec 2017 16:24:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446073#M35022</guid>
      <dc:creator>TedKowal</dc:creator>
      <dc:date>2017-12-11T16:24:33Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator - Diameter conversion with conditions</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446074#M35023</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;move to Pro and Python 3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unicode has been covered by python since 3 and I haven't suffered with Unicode issues in over 8 years &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 11 Dec 2017 16:45:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446074#M35023</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-12-11T16:45:12Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator - Diameter conversion with conditions</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446075#M35024</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks everyone this worked great. Fantastic!! I'm off to learn some Python!!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Dec 2017 07:58:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446075#M35024</guid>
      <dc:creator>THomasDanks</dc:creator>
      <dc:date>2017-12-12T07:58:41Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator - Diameter conversion with conditions</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446076#M35025</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Love to move to Pro but it does not accept MS Access which is critical to our operations.&amp;nbsp; The choice of DB's is not mine to make!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Dec 2017 15:04:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-diameter-conversion-with/m-p/446076#M35025</guid>
      <dc:creator>TedKowal</dc:creator>
      <dc:date>2017-12-12T15:04:33Z</dc:date>
    </item>
  </channel>
</rss>

