<?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: Fix O' names using Python in Field Calculator in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164274#M54027</link>
    <description>&lt;P&gt;&lt;A href="https://github.com/ppannuto/python-titlecase" target="_blank"&gt;https://github.com/ppannuto/python-titlecase&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 14 Apr 2022 07:01:55 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2022-04-14T07:01:55Z</dc:date>
    <item>
      <title>Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164108#M54009</link>
      <description>&lt;P&gt;Very simply if I have the following sentence:&lt;/P&gt;&lt;P&gt;"RYAN O'NEAL AND JERRY O'CONNELL WERE ON CONAN O'BRIEN."&lt;/P&gt;&lt;P&gt;I want to get&lt;/P&gt;&lt;P&gt;"Ryan O'Neal and Jerry O'Connell were on Conan O'Brien."&lt;/P&gt;&lt;P&gt;I would like to accomplish this with Python in the Field Calculator....&lt;/P&gt;&lt;P&gt;I can get O' so close (get it?), but&amp;nbsp;I keep getting it O' so wrong (last one I promise)&lt;/P&gt;&lt;P&gt;Thank you for your time.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2022 20:51:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164108#M54009</guid>
      <dc:creator>KimberlyGarbade</dc:creator>
      <dc:date>2022-04-13T20:51:04Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164120#M54012</link>
      <description>&lt;P&gt;normally "title" but you have to de-title the non-names&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"{} and {} were on {}".format(*[i.title() for i in names])
"Ryan O'Neal and Jerry O'Connell were on Conan O'Brien"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;leaving you to build your sentence from a list of names and sentence fluff as you go&amp;nbsp;&lt;/P&gt;&lt;P&gt;BUT in the field calculator... not really, just "title" the string, at least it looks O'somuchbetter than the full caps&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2022 20:50:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164120#M54012</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-04-13T20:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164135#M54013</link>
      <description>&lt;P&gt;What you are after is called true casing/truecasing, and it is a natural language processing problem. &amp;nbsp; The Python built-in string type does not have any method for doing such handling of a string.&amp;nbsp; There are quite a few Python packages that do truecasing, but you would have to install them and then write a fairly involved code block expression.&lt;/P&gt;&lt;P&gt;I am with Dan, &lt;FONT face="courier new,courier"&gt;str.title()&lt;/FONT&gt; is probably the best compromise in terms of effort to results.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2022 21:10:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164135#M54013</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-04-13T21:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164143#M54016</link>
      <description>&lt;P&gt;I had issues like this with street names.&amp;nbsp; Luckily, the list of names that don't play well with str.title() is fairly small in my case so I made a dictionary of them.&lt;/P&gt;&lt;P&gt;This is the code I use for that, maybe you can get an idea (or, maybe even come up with a way to populate the dictionary with python (split at the ', title() the splits, then append them back together as titlecased or something).&lt;/P&gt;&lt;P&gt;Some extra code in here as this code also makes sure streets with N,S, etc. stay capitalized, and streets like 1st street aren't (1ST) (so that the upperWords and lowerWords makes sense).&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def CalcStreetLabels():
    fields =  ['DirectPrefix', 'StreetName','StreetLabels']
    upperWords = ["N","NW","W","SW","S","SE","E","NE","US","PR"]
    lowerWords = ["1ST","2ND","3RD","4TH","5TH","6TH","7TH","8TH","9TH","10TH","11TH","12TH","13TH"]

    subDict = { 
                "MCMURRAY ST": "McMurray St",
                "MCMURRAY AVE": "McMurray Ave",
                "MCINTOSH CT": "McIntosh Ct",
                "MCEWAN DR": "McEwan Dr",
                "MCPHERSON AVE": "McPherson Ave",
                "MCCLELLAN ST": "McClellen St",
                "BY-PASS": "Bypass Hwy         SR 240",
                "SR 240": "SR 240",
                "O'CONNOR ST":"O'Connor St",
                "MCMURRAY": "McMurray",
                "MCINTOSH": "McIntosh",
                "MCEWAN": "McEwan",
                "MCPHERSON": "McPherson",
                "MCCLELLAN": "McClellen",
                "O'CONNOR":"O'Connor"
                }
    with arcpy.da.UpdateCursor(infc,fields) as uCur:
        words = [] 
        for row in uCur:
                    label = ""
       
                    if row[1]:
                       words =((' '.join(filter(None, [row[0], row[1]]))).split(" "))
                    for word in words:
                        if word in upperWords:
                            label += word.upper() + " "
                        elif word in lowerWords:
                            label += word.lower() + " "
                        else:
                           label += str(word).title() + " "
                    if subDict.has_key(row[1]):
                        label = subDict[row[1]]
                    row[2] = label   
                    if label:
                       uCur.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In case it helps any,&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2022 21:23:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164143#M54016</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-04-13T21:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164145#M54017</link>
      <description>&lt;P&gt;I see the value of what you are saying.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was hoping to use something like this, but its running up against the limits of my understanding of regular expressions in Python.&lt;/P&gt;&lt;P&gt;I'm just so close I don't want to give up on it.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import re

instring = "RYAN O'NEAL AND JERRY O'CONNELL WERE ON CONAN O'BRIEN."
print("Original: " + instring)

instring = instring.capitalize()
print("Sentence Case: " + instring)

if re.search(r"\b"+"O'"+'[a-z]', instring, re.IGNORECASE): instring = re.sub("(^'O'|['])\s*([a-zA-Z])", lambda p: p.group(0).upper(), instring)

print("Correct O\' names: " + instring)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It gets me here (so close but yet so far).&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="KimGarbade_0-1649885010394.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/38833i88809C3F135BC01E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="KimGarbade_0-1649885010394.png" alt="KimGarbade_0-1649885010394.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2022 21:24:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164145#M54017</guid>
      <dc:creator>KimberlyGarbade</dc:creator>
      <dc:date>2022-04-13T21:24:45Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164184#M54018</link>
      <description>&lt;P&gt;Not an re guy, but this seems to work with my street names:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

fc = r'C:\_ESRI\GDB\Landbase.gdb\Centerlines'       # Feature class with table
field = 'StreetName'                                # Field I want to update
ll=[]                                               # Empty list for holding name


with arcpy.da.UpdateCursor(fc,field) as cursor:
    for row in cursor:
        if "'" in row[0]:                           # see if apostrophe is in the name
            ll.append(row[0])                       # if so, append to empty list
            for w in ll:
                g = w.split("'")                    # Split the name at the apostrophe
            row[0] = g[0].title() + "'" + g[1].title()   # concatenate them back together as title() with apostrophe in between
            cursor.updateRow(row)                   # update the name
            ll = []                                 # empty the list for next value&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course, this operates on a single field with the name.&amp;nbsp; But, should be able to modify to iterate through the words in a sentence also.&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2022 23:12:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164184#M54018</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-04-13T23:12:43Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164203#M54020</link>
      <description>&lt;P&gt;This appears to be for an exercise, so does the solution &lt;U&gt;only&lt;/U&gt; need to work for this one sentence, or does it need to be generally applicable to strings?&amp;nbsp; If the former, than you can do something like:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; s = "RYAN O'NEAL AND JERRY O'CONNELL WERE ON CONAN O'BRIEN."
&amp;gt;&amp;gt;&amp;gt; lower_list = ["and", "were", "on"]
&amp;gt;&amp;gt;&amp;gt; " ".join(
...   i.lower() if i.lower() in lower_list else i.title()
...   for i in s.split()
... )
"Ryan O'Neal and Jerry O'Connell were on Conan O'Brien."
&amp;gt;&amp;gt;&amp;gt; &lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 14 Apr 2022 00:01:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164203#M54020</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-04-14T00:01:08Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164213#M54021</link>
      <description>&lt;P&gt;All O' names in this example, but ideally a solution that can be applied to all names that start with more than just a capital letter (I.E. Mc, O', St., etc)&amp;nbsp; I have thousands of comment fields that were traditionally all caps. We are transitioning to sentence case. The comments have a lot of street and person names embedded, so we wind up with things like "June paton stated it was 100 ft of rpc along the west side of 1st st." ... sure you can read it, but it looks wrong. I've corrected a bunch with "exception" lists (basically search and replace pairs of if you find this replace it with this) but names are proving difficult since Bill and water bill are two different things.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Apr 2022 00:29:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164213#M54021</guid>
      <dc:creator>KimberlyGarbade</dc:creator>
      <dc:date>2022-04-14T00:29:33Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164217#M54022</link>
      <description>&lt;P&gt;I have very similar code for the exceptions and lower and upper, but I like your if subDict.has_key approach better than my for x in approach so I'm stealing that. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Apr 2022 00:40:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164217#M54022</guid>
      <dc:creator>KimberlyGarbade</dc:creator>
      <dc:date>2022-04-14T00:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164274#M54027</link>
      <description>&lt;P&gt;&lt;A href="https://github.com/ppannuto/python-titlecase" target="_blank"&gt;https://github.com/ppannuto/python-titlecase&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Apr 2022 07:01:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164274#M54027</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2022-04-14T07:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164362#M54043</link>
      <description>&lt;P&gt;That Python package expands, a little, on the built-in &lt;FONT face="courier new,courier"&gt;str.title()&lt;/FONT&gt; functionality so I don't know if it really offers much here since the OP is after truecase conversion.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Apr 2022 12:50:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1164362#M54043</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-04-14T12:50:59Z</dc:date>
    </item>
    <item>
      <title>Re: Fix O' names using Python in Field Calculator</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1167476#M54394</link>
      <description>&lt;P&gt;I finally had a chance to circle back on this one.&lt;/P&gt;&lt;P&gt;This code works for "O'" names but like some many have discovered before me when you are dealing with hundreds of thousands of comments and 60,000 street names and embedded organization acronyms and proper names.... there are just too many exceptions to handle.&amp;nbsp; I think the solution might just be title case and call it a day.&amp;nbsp; Not optimum, but trying to achieve optimum is probably wasted effort in this case.&amp;nbsp; At least I got to mess around with re and lambda functions.&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import re

test = "RYAN O'NEAL AND JERRY O'CONNELL WERE ON CONAN O'BRIEN."
test2 = test.capitalize()

wholeExceptions = [
   ("jerry","Jerry"),
   ("ryan","Ryan"),
   ("conan","Conan")
]


def subsmade (instring):


  #Handle exception that are whole words
  for x in wholeExceptions:
      if re.search(r"\b" + re.escape(x[0]) + r"\b", instring): 
          instring = re.sub(r'\b'+x[0]+r'\b',x[1],instring)

      

  if re.search(r"\b"+"O'"+'[a-z]', instring, re.IGNORECASE): instring = re.sub(r"([o]['][a-zA-Z]|[O]['][a-z])", lambda p: p.group(0).upper(), instring)
  return instring

print (subsmade(test2))
# prints "Ryan O'Neal and Jerry O'Connell were on Conan O'Brien."&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Apr 2022 13:39:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/fix-o-names-using-python-in-field-calculator/m-p/1167476#M54394</guid>
      <dc:creator>KimberlyGarbade</dc:creator>
      <dc:date>2022-04-25T13:39:07Z</dc:date>
    </item>
  </channel>
</rss>

