Splitting string ID code into various partsSplitting string in Python parser of ArcGIS Field Calculator?Select polygons contained inside a polygon and assign IDHow to place points along a line in a specific offset using python / arcpy?Help with formatting ArcGIS text elements with PythonSplitting string column into 2 columns in Python?Find the first occurrence of any letter in an alphanumeric stringRemove first seven characters from field string using Field CalculatorHow can I convert Bing's “quadtree” tile addresses to ZXY tile addresses in Python?Address Prefix Strip Using PythonOverlay two linestring objects in geopandas, accounting for the attributes

Importance of differentiation

What has been your most complicated TikZ drawing?

Replacing Windows 7 security updates with anti-virus?

When do we add an hyphen (-) to a complex adjective word?

Current sense amp + op-amp buffer + ADC: Measuring down to 0 with single supply

Does this AnyDice function accurately calculate the number of ogres you make unconcious with three 4th-level castings of Sleep?

Splitting string ID code into various parts

What is IP squat space

What is the greatest age difference between a married couple in Tanach?

Can elves maintain concentration in a trance?

Why do passenger jet manufacturers design their planes with stall prevention systems?

The use of "touch" and "touch on" in context

How could a female member of a species produce eggs unto death?

Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?

PTIJ: Who should pay for Uber rides: the child or the parent?

Is it normal that my co-workers at a fitness company criticize my food choices?

Define, (actually define) the "stability" and "energy" of a compound

Why did it take so long to abandon sail after steamships were demonstrated?

Life insurance that covers only simultaneous/dual deaths

Running a subshell from the middle of the current command

How to make healing in an exploration game interesting

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

Ban on all campaign finance?

Have researchers managed to "reverse time"? If so, what does that mean for physics?



Splitting string ID code into various parts


Splitting string in Python parser of ArcGIS Field Calculator?Select polygons contained inside a polygon and assign IDHow to place points along a line in a specific offset using python / arcpy?Help with formatting ArcGIS text elements with PythonSplitting string column into 2 columns in Python?Find the first occurrence of any letter in an alphanumeric stringRemove first seven characters from field string using Field CalculatorHow can I convert Bing's “quadtree” tile addresses to ZXY tile addresses in Python?Address Prefix Strip Using PythonOverlay two linestring objects in geopandas, accounting for the attributes













1















I have a series of identification codes that I need to split out. The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].



An example of some codes includes S22-201, TT100-12, and V6-1B. Often there is no subdistrict, and all points fall within the same larger district (so no As or Cs or whatever at the end of the string.



I can do parts of it, like splitting at the hyphen.



!Original_ID!.split('-')[0]


and then extracting the district



!Split_ID![1:3]


But it seems like two steps for this are unnecessary, and only works when I know the specific number of characters in the string, which isn't realistic for a large data set.



I'd like to be able to grab each piece at once:



  • letters on the left of the hyphen

  • numbers on the left of the hyphen

  • numbers on the right of the hyphen

  • letters (if any) on the right of the hyphen.

I'd need the numeric fields to be integers (or I guess possibly floats in some rare cases maybe).










share|improve this question









New contributor




vce500 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    1















    I have a series of identification codes that I need to split out. The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].



    An example of some codes includes S22-201, TT100-12, and V6-1B. Often there is no subdistrict, and all points fall within the same larger district (so no As or Cs or whatever at the end of the string.



    I can do parts of it, like splitting at the hyphen.



    !Original_ID!.split('-')[0]


    and then extracting the district



    !Split_ID![1:3]


    But it seems like two steps for this are unnecessary, and only works when I know the specific number of characters in the string, which isn't realistic for a large data set.



    I'd like to be able to grab each piece at once:



    • letters on the left of the hyphen

    • numbers on the left of the hyphen

    • numbers on the right of the hyphen

    • letters (if any) on the right of the hyphen.

    I'd need the numeric fields to be integers (or I guess possibly floats in some rare cases maybe).










    share|improve this question









    New contributor




    vce500 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      1












      1








      1








      I have a series of identification codes that I need to split out. The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].



      An example of some codes includes S22-201, TT100-12, and V6-1B. Often there is no subdistrict, and all points fall within the same larger district (so no As or Cs or whatever at the end of the string.



      I can do parts of it, like splitting at the hyphen.



      !Original_ID!.split('-')[0]


      and then extracting the district



      !Split_ID![1:3]


      But it seems like two steps for this are unnecessary, and only works when I know the specific number of characters in the string, which isn't realistic for a large data set.



      I'd like to be able to grab each piece at once:



      • letters on the left of the hyphen

      • numbers on the left of the hyphen

      • numbers on the right of the hyphen

      • letters (if any) on the right of the hyphen.

      I'd need the numeric fields to be integers (or I guess possibly floats in some rare cases maybe).










      share|improve this question









      New contributor




      vce500 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I have a series of identification codes that I need to split out. The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].



      An example of some codes includes S22-201, TT100-12, and V6-1B. Often there is no subdistrict, and all points fall within the same larger district (so no As or Cs or whatever at the end of the string.



      I can do parts of it, like splitting at the hyphen.



      !Original_ID!.split('-')[0]


      and then extracting the district



      !Split_ID![1:3]


      But it seems like two steps for this are unnecessary, and only works when I know the specific number of characters in the string, which isn't realistic for a large data set.



      I'd like to be able to grab each piece at once:



      • letters on the left of the hyphen

      • numbers on the left of the hyphen

      • numbers on the right of the hyphen

      • letters (if any) on the right of the hyphen.

      I'd need the numeric fields to be integers (or I guess possibly floats in some rare cases maybe).







      python arcmap field-calculator






      share|improve this question









      New contributor




      vce500 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      vce500 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 21 mins ago









      Taras

      2,2342727




      2,2342727






      New contributor




      vce500 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 hours ago









      vce500vce500

      62




      62




      New contributor




      vce500 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      vce500 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      vce500 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes


















          2














          You're not going to be able to calculate two fields in one go.. though you can split it up into two calcs. I would do this with an update cursor:



          with arcpy.da.UpdateCursor(YourFeatureClass,['Original_ID','District','Split_ID']) as uCur:
          for sRow in uCur:
          OrigID = sRow[0].split('-')[0] # first element in the Original_ID
          charRng = range(len(OrigID)) # a range to iterate over
          Chars = ''
          Numbers = ''
          for Idx in charRng:
          if OrigID[Idx].isnumeric():
          Numbers += OrigID[Idx]
          else:
          chars += OrigID[Idx]
          sRow[1] = float(Numbers)
          sRow[2] = Chars
          uCur.updateRow(sRow)


          This shows how to break up a string into numbers and not numbers and put the values into a row, it should give you some ideas where to start from.






          share|improve this answer






























            1














            Assuming you have four fields, region, district, place and subdistrict already added and you want to use the field calculator to populate them. You would have to run the calculator four times using an expression like:



            Code Block



            import re
            def parse(s):
            """The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].
            An example of a some codes include S22-201, TT100-12, and V6-1B.
            Often there is no subdistrict, and all points fall within the same larger district
            (so no As or Cs or whatever at the end of the string)."""

            letters = re.findall(r'[a-z A-Z]+', s)
            numbers = re.findall(r'[0-9]+', s)

            region = letters[0]
            district, place = [int(n) for n in numbers]
            try:
            subdistrict = letters[1]
            except IndexError:
            subdistrict = None

            return region, district, place, subdistrict


            Then for the region field, use:



            parse(!Original_ID!)[0]


            For district:



            parse(!Original_ID!)[1]


            For place:



            parse(!Original_ID!)[2]


            For subdistrict:



            parse(!Original_ID!)[3]


            However, I would use the update cursor approach suggested by Michael Stimson so you could update all four fields in one hit. Use the following in the python window of ArcMap/ArcGIS Pro:



            import re 
            def parse(s):
            etc... from code block above

            with arcpy.da.UpdateCursor(YourFeatureClass, ['Original_ID','Region', 'District', 'Place', 'Subdistrict']) as rows:
            for row in rows:
            region, district, place, subdistrict = parse(row[0])
            row = [row[0], region, district, place, subdistrict]
            rows.updateRow(row)





            share|improve this answer
























              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "79"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );






              vce500 is a new contributor. Be nice, and check out our Code of Conduct.









              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f315591%2fsplitting-string-id-code-into-various-parts%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              You're not going to be able to calculate two fields in one go.. though you can split it up into two calcs. I would do this with an update cursor:



              with arcpy.da.UpdateCursor(YourFeatureClass,['Original_ID','District','Split_ID']) as uCur:
              for sRow in uCur:
              OrigID = sRow[0].split('-')[0] # first element in the Original_ID
              charRng = range(len(OrigID)) # a range to iterate over
              Chars = ''
              Numbers = ''
              for Idx in charRng:
              if OrigID[Idx].isnumeric():
              Numbers += OrigID[Idx]
              else:
              chars += OrigID[Idx]
              sRow[1] = float(Numbers)
              sRow[2] = Chars
              uCur.updateRow(sRow)


              This shows how to break up a string into numbers and not numbers and put the values into a row, it should give you some ideas where to start from.






              share|improve this answer



























                2














                You're not going to be able to calculate two fields in one go.. though you can split it up into two calcs. I would do this with an update cursor:



                with arcpy.da.UpdateCursor(YourFeatureClass,['Original_ID','District','Split_ID']) as uCur:
                for sRow in uCur:
                OrigID = sRow[0].split('-')[0] # first element in the Original_ID
                charRng = range(len(OrigID)) # a range to iterate over
                Chars = ''
                Numbers = ''
                for Idx in charRng:
                if OrigID[Idx].isnumeric():
                Numbers += OrigID[Idx]
                else:
                chars += OrigID[Idx]
                sRow[1] = float(Numbers)
                sRow[2] = Chars
                uCur.updateRow(sRow)


                This shows how to break up a string into numbers and not numbers and put the values into a row, it should give you some ideas where to start from.






                share|improve this answer

























                  2












                  2








                  2







                  You're not going to be able to calculate two fields in one go.. though you can split it up into two calcs. I would do this with an update cursor:



                  with arcpy.da.UpdateCursor(YourFeatureClass,['Original_ID','District','Split_ID']) as uCur:
                  for sRow in uCur:
                  OrigID = sRow[0].split('-')[0] # first element in the Original_ID
                  charRng = range(len(OrigID)) # a range to iterate over
                  Chars = ''
                  Numbers = ''
                  for Idx in charRng:
                  if OrigID[Idx].isnumeric():
                  Numbers += OrigID[Idx]
                  else:
                  chars += OrigID[Idx]
                  sRow[1] = float(Numbers)
                  sRow[2] = Chars
                  uCur.updateRow(sRow)


                  This shows how to break up a string into numbers and not numbers and put the values into a row, it should give you some ideas where to start from.






                  share|improve this answer













                  You're not going to be able to calculate two fields in one go.. though you can split it up into two calcs. I would do this with an update cursor:



                  with arcpy.da.UpdateCursor(YourFeatureClass,['Original_ID','District','Split_ID']) as uCur:
                  for sRow in uCur:
                  OrigID = sRow[0].split('-')[0] # first element in the Original_ID
                  charRng = range(len(OrigID)) # a range to iterate over
                  Chars = ''
                  Numbers = ''
                  for Idx in charRng:
                  if OrigID[Idx].isnumeric():
                  Numbers += OrigID[Idx]
                  else:
                  chars += OrigID[Idx]
                  sRow[1] = float(Numbers)
                  sRow[2] = Chars
                  uCur.updateRow(sRow)


                  This shows how to break up a string into numbers and not numbers and put the values into a row, it should give you some ideas where to start from.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 5 hours ago









                  Michael StimsonMichael Stimson

                  21.6k22360




                  21.6k22360























                      1














                      Assuming you have four fields, region, district, place and subdistrict already added and you want to use the field calculator to populate them. You would have to run the calculator four times using an expression like:



                      Code Block



                      import re
                      def parse(s):
                      """The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].
                      An example of a some codes include S22-201, TT100-12, and V6-1B.
                      Often there is no subdistrict, and all points fall within the same larger district
                      (so no As or Cs or whatever at the end of the string)."""

                      letters = re.findall(r'[a-z A-Z]+', s)
                      numbers = re.findall(r'[0-9]+', s)

                      region = letters[0]
                      district, place = [int(n) for n in numbers]
                      try:
                      subdistrict = letters[1]
                      except IndexError:
                      subdistrict = None

                      return region, district, place, subdistrict


                      Then for the region field, use:



                      parse(!Original_ID!)[0]


                      For district:



                      parse(!Original_ID!)[1]


                      For place:



                      parse(!Original_ID!)[2]


                      For subdistrict:



                      parse(!Original_ID!)[3]


                      However, I would use the update cursor approach suggested by Michael Stimson so you could update all four fields in one hit. Use the following in the python window of ArcMap/ArcGIS Pro:



                      import re 
                      def parse(s):
                      etc... from code block above

                      with arcpy.da.UpdateCursor(YourFeatureClass, ['Original_ID','Region', 'District', 'Place', 'Subdistrict']) as rows:
                      for row in rows:
                      region, district, place, subdistrict = parse(row[0])
                      row = [row[0], region, district, place, subdistrict]
                      rows.updateRow(row)





                      share|improve this answer





























                        1














                        Assuming you have four fields, region, district, place and subdistrict already added and you want to use the field calculator to populate them. You would have to run the calculator four times using an expression like:



                        Code Block



                        import re
                        def parse(s):
                        """The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].
                        An example of a some codes include S22-201, TT100-12, and V6-1B.
                        Often there is no subdistrict, and all points fall within the same larger district
                        (so no As or Cs or whatever at the end of the string)."""

                        letters = re.findall(r'[a-z A-Z]+', s)
                        numbers = re.findall(r'[0-9]+', s)

                        region = letters[0]
                        district, place = [int(n) for n in numbers]
                        try:
                        subdistrict = letters[1]
                        except IndexError:
                        subdistrict = None

                        return region, district, place, subdistrict


                        Then for the region field, use:



                        parse(!Original_ID!)[0]


                        For district:



                        parse(!Original_ID!)[1]


                        For place:



                        parse(!Original_ID!)[2]


                        For subdistrict:



                        parse(!Original_ID!)[3]


                        However, I would use the update cursor approach suggested by Michael Stimson so you could update all four fields in one hit. Use the following in the python window of ArcMap/ArcGIS Pro:



                        import re 
                        def parse(s):
                        etc... from code block above

                        with arcpy.da.UpdateCursor(YourFeatureClass, ['Original_ID','Region', 'District', 'Place', 'Subdistrict']) as rows:
                        for row in rows:
                        region, district, place, subdistrict = parse(row[0])
                        row = [row[0], region, district, place, subdistrict]
                        rows.updateRow(row)





                        share|improve this answer



























                          1












                          1








                          1







                          Assuming you have four fields, region, district, place and subdistrict already added and you want to use the field calculator to populate them. You would have to run the calculator four times using an expression like:



                          Code Block



                          import re
                          def parse(s):
                          """The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].
                          An example of a some codes include S22-201, TT100-12, and V6-1B.
                          Often there is no subdistrict, and all points fall within the same larger district
                          (so no As or Cs or whatever at the end of the string)."""

                          letters = re.findall(r'[a-z A-Z]+', s)
                          numbers = re.findall(r'[0-9]+', s)

                          region = letters[0]
                          district, place = [int(n) for n in numbers]
                          try:
                          subdistrict = letters[1]
                          except IndexError:
                          subdistrict = None

                          return region, district, place, subdistrict


                          Then for the region field, use:



                          parse(!Original_ID!)[0]


                          For district:



                          parse(!Original_ID!)[1]


                          For place:



                          parse(!Original_ID!)[2]


                          For subdistrict:



                          parse(!Original_ID!)[3]


                          However, I would use the update cursor approach suggested by Michael Stimson so you could update all four fields in one hit. Use the following in the python window of ArcMap/ArcGIS Pro:



                          import re 
                          def parse(s):
                          etc... from code block above

                          with arcpy.da.UpdateCursor(YourFeatureClass, ['Original_ID','Region', 'District', 'Place', 'Subdistrict']) as rows:
                          for row in rows:
                          region, district, place, subdistrict = parse(row[0])
                          row = [row[0], region, district, place, subdistrict]
                          rows.updateRow(row)





                          share|improve this answer















                          Assuming you have four fields, region, district, place and subdistrict already added and you want to use the field calculator to populate them. You would have to run the calculator four times using an expression like:



                          Code Block



                          import re
                          def parse(s):
                          """The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].
                          An example of a some codes include S22-201, TT100-12, and V6-1B.
                          Often there is no subdistrict, and all points fall within the same larger district
                          (so no As or Cs or whatever at the end of the string)."""

                          letters = re.findall(r'[a-z A-Z]+', s)
                          numbers = re.findall(r'[0-9]+', s)

                          region = letters[0]
                          district, place = [int(n) for n in numbers]
                          try:
                          subdistrict = letters[1]
                          except IndexError:
                          subdistrict = None

                          return region, district, place, subdistrict


                          Then for the region field, use:



                          parse(!Original_ID!)[0]


                          For district:



                          parse(!Original_ID!)[1]


                          For place:



                          parse(!Original_ID!)[2]


                          For subdistrict:



                          parse(!Original_ID!)[3]


                          However, I would use the update cursor approach suggested by Michael Stimson so you could update all four fields in one hit. Use the following in the python window of ArcMap/ArcGIS Pro:



                          import re 
                          def parse(s):
                          etc... from code block above

                          with arcpy.da.UpdateCursor(YourFeatureClass, ['Original_ID','Region', 'District', 'Place', 'Subdistrict']) as rows:
                          for row in rows:
                          region, district, place, subdistrict = parse(row[0])
                          row = [row[0], region, district, place, subdistrict]
                          rows.updateRow(row)






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 2 hours ago

























                          answered 2 hours ago









                          user2856user2856

                          30.3k258105




                          30.3k258105




















                              vce500 is a new contributor. Be nice, and check out our Code of Conduct.









                              draft saved

                              draft discarded


















                              vce500 is a new contributor. Be nice, and check out our Code of Conduct.












                              vce500 is a new contributor. Be nice, and check out our Code of Conduct.











                              vce500 is a new contributor. Be nice, and check out our Code of Conduct.














                              Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f315591%2fsplitting-string-id-code-into-various-parts%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Are there any AGPL-style licences that require source code modifications to be public? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Force derivative works to be publicAre there any GPL like licenses for Apple App Store?Do you violate the GPL if you provide source code that cannot be compiled?GPL - is it distribution to use libraries in an appliance loaned to customers?Distributing App for free which uses GPL'ed codeModifications of server software under GPL, with web/CLI interfaceDoes using an AGPLv3-licensed library prevent me from dual-licensing my own source code?Can I publish only select code under GPLv3 from a private project?Is there published precedent regarding the scope of covered work that uses AGPL software?If MIT licensed code links to GPL licensed code what should be the license of the resulting binary program?If I use a public API endpoint that has its source code licensed under AGPL in my app, do I need to disclose my source?

                              2013 GY136 Descoberta | Órbita | Referências Menu de navegação«List Of Centaurs and Scattered-Disk Objects»«List of Known Trans-Neptunian Objects»

                              Button changing it's text & action. Good or terrible? The 2019 Stack Overflow Developer Survey Results Are Inchanging text on user mouseoverShould certain functions be “hard to find” for powerusers to discover?Custom liking function - do I need user login?Using different checkbox style for different checkbox behaviorBest Practices: Save and Exit in Software UIInteraction with remote validated formMore efficient UI to progress the user through a complicated process?Designing a popup notice for a gameShould bulk-editing functions be hidden until a table row is selected, or is there a better solution?Is it bad practice to disable (replace) the context menu?