<?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 Inserts a &amp;quot;u&amp;quot; In My Function in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518919#M40639</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here's the whole shebang, then:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def fix(oldApn):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if '-' not in oldApn:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newApn = ''
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; apnList = oldApn.split('-')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(apnList) == 3:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newApn = apnList[0].lstrip() + '-' + apnList[1].lstrip + '-' + apnList[2].zfill(2)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newApn = apnList[0].lstrip() + '-' + apnList[1].lstrip
&amp;nbsp;&amp;nbsp;&amp;nbsp; return newApn&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It's supposed to make something like "EBRI-000001-000000-000003" into "1-3" or "EBRI-000001-000000-000004-000001" into "1-4-1".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Instead, I get Error 000539: Error running expression: fix(u"EBRI-000001-000000-000003").&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 22:38:17 GMT</pubDate>
    <dc:creator>RussellTaylor</dc:creator>
    <dc:date>2021-12-11T22:38:17Z</dc:date>
    <item>
      <title>Field Calculator Inserts a &amp;amp;quot;u&amp;amp;quot; In My Function</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518915#M40635</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Sometimes when I'm using a Codeblock in Field Calculator, I get an ERROR 000539 which shows something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;function_name(u"fieldvalue")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, that "u" isn't in the Codeblock OR the regular Field Calculator box. Where is it coming from?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Jun 2014 14:01:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518915#M40635</guid>
      <dc:creator>RussellTaylor</dc:creator>
      <dc:date>2014-06-05T14:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Inserts a "u" In My Function</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518916#M40636</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The u is a flag that identifies the string as unicode.&amp;nbsp; it's a python thing.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;an ERROR 000539 is a generic Arc code for a python error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;perhaps that function will not accept a string (to wit: "fieldvalue") as an input.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;at a guess, fieldvalue looks like you expect it to be a variable, but it is being passed as a string of the variable name&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Jun 2014 14:09:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518916#M40636</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2014-06-05T14:09:12Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Inserts a "u" In My Function</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518917#M40637</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;To be a bit clearer, I'm formatting string attributes using a function I've defined in the Codeblock. It has 1 argument, the input field, and outputs to a different field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I run fix(!PIN!) and get back fix(u"CARV-000001-000001-B000000") in the error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I generalized to functionname and fieldvalue because I've had this same problem several times before, and while I can usually get it to work after playing with it for a while, I'm curious as to why it happens and the best way to avoid/deal with it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Jun 2014 14:22:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518917#M40637</guid>
      <dc:creator>RussellTaylor</dc:creator>
      <dc:date>2014-06-05T14:22:36Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Inserts a "u" In My Function</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518918#M40638</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, it was pretty clear about using a codeblock,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and that python cannot understand how to use the input argument.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It is not helpful to 'generalize' the inputs, or to not bother with being explicit&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;about the function code or the field values being fed to it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It is still not clear what the problem might be:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I assume "CARV-000001-000001-B000000" is a value in the field PIN for a particular row...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(if it is not, well, that could be a problem too)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Obviously the function is not expecting that particular string: why? &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;we don't know; we don't know what the function is trying to do&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(beyond that it will try to "format" it).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;One does not have a lot to go on here...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Except that, as I wrote before, the prefixed u is not (at least in itself) the problem.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Jun 2014 11:44:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518918#M40638</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2014-06-06T11:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Inserts a "u" In My Function</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518919#M40639</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here's the whole shebang, then:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def fix(oldApn):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if '-' not in oldApn:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newApn = ''
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; apnList = oldApn.split('-')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(apnList) == 3:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newApn = apnList[0].lstrip() + '-' + apnList[1].lstrip + '-' + apnList[2].zfill(2)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newApn = apnList[0].lstrip() + '-' + apnList[1].lstrip
&amp;nbsp;&amp;nbsp;&amp;nbsp; return newApn&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It's supposed to make something like "EBRI-000001-000000-000003" into "1-3" or "EBRI-000001-000000-000004-000001" into "1-4-1".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Instead, I get Error 000539: Error running expression: fix(u"EBRI-000001-000000-000003").&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:38:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518919#M40639</guid>
      <dc:creator>RussellTaylor</dc:creator>
      <dc:date>2021-12-11T22:38:17Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Inserts a "u" In My Function</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518920#M40640</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;neither apnList[0].lstrip() nor apnList[1].lstrip&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(with or without the ())&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;will strip off the leading zeros. The default is to strip whitespace.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;apnList[0].lstrip('0') will strip zeros, as will&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;str(int(apnList[0]))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;but the second option will preserve a single zero, instead of returning a blank string&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I don't know what you want the zfill(2) to do... perhaps it is a typo?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Your logic in the length of apnList test is a little odd.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;first off, does a length test of 3-or-else cover all (or any of) your possible use cases?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It does not look like it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;then, your parser takes items 0, 1, &amp;amp; 2&amp;nbsp; (or 0 &amp;amp; 1) from apnList ...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;spliting "EBRI-000001-000000-000003" on the '-' gives ['EBRI', '000001', '000000', '000003']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;so the length will be 4, and taking 0, 1 &amp;amp; 2 will give you&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;EBRI-1-&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(if you use your lstrip parsing)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;nether of your parsing options will turn&amp;nbsp; "EBRI-000001-000000-000003" into "1-3" &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try debugging your function in IDLE by just passing it some sample strings.....&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 09 Jun 2014 11:36:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518920#M40640</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2014-06-09T11:36:14Z</dc:date>
    </item>
    <item>
      <title>Re: Field Calculator Inserts a "u" In My Function</title>
      <link>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518921#M40641</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here's how I would do it, using lists and list comprehensions...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14116299022659868 jive_text_macro" jivemacro_uid="_14116299022659868" modifiedtitle="true"&gt;
&lt;P&gt;def fix(oldApn):&amp;nbsp; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; # find integer pieces in "-" delimited string, skipping zeros&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; pieces = [str(int(p)) for p in oldApn.split("-") if p.isdigit()]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; newApn = "-".join([p for p in pieces if p != "0"])&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; return newAPn&lt;/P&gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Sep 2014 07:26:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/field-calculator-inserts-a-amp-amp-quot-u-amp-amp/m-p/518921#M40641</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-09-25T07:26:39Z</dc:date>
    </item>
  </channel>
</rss>

