Splitting ID number along hyphens and creating new field with only parts of ID In Field Calculator?Splitting...

Cat is tipping over bed-side lamps during the night

Lightning Data Table inline edit

Coworker asking me to not bring cakes due to self control issue. What should I do?

What does MTU depend on?

Eww, those bytes are gross

Renting a 2CV in France

What is the industry term for house wiring diagrams?

How do you funnel food off a cutting board?

Possible issue with my W4 and tax return

Why avoid shared user accounts?

Which RAF squadrons and aircraft types took part in the bombing of Berlin on the 25th of August 1940?

Can a player sacrifice a creature after declaring that creature as blocker while taking lethal damage?

How is this property called for mod?

Critique vs nitpicking

Concatenating two int[]

Does the US government have any planning in place to ensure there's no shortages of food, fuel, steel and other commodities?

Why does 0.-5 evaluate to -5?

Equivalent of "illegal" for violating civil law

Crack the bank account's password!

A question about partitioning positivie integers into finitely many arithmetic progresions

Book where a space ship journeys to the center of the galaxy to find all the stars had gone supernova

How to not let the Identify spell spoil everything?

Word for something that's always reliable, but never the best?

What is a good reason for every spaceship to carry a weapon on board?



Splitting ID number along hyphens and creating new field with only parts of ID In Field Calculator?


Splitting string in Python parser of ArcGIS Field Calculator?Use Round in ArcMap 10.x Field Calculator on field with both numeric and NULL valuesRemoving return/newline (n) character from Field using Python and Field Calculator?Using python elif syntax in Field Calculator to reclassify a range of values from a different field and populate a new fieldGetting Null value error with Python Parser in field calculator?Autopopulating text field with string and number combinationUsing ArcMap Field Calculator to count number of identical values?Sequential IDs with field calculator: Pad a prefixed field to specific lengthReplacing values with non-English characters in attribute table field using ArcGIS field calculator and python parser?Using ArcGIS Field calculator with global variable?













1















I have a shapefile with polygons and each polygon has an ID number that follows a pattern like this:



SMP_ID
1077-1-1
8-2-2
1281-1-3
304-2-1


In have created a new field and I need to populate it with a truncated version of these IDs, which would look like this:



SYS_ID
1077-1
8-2
1281-1
304-2


This is the code I've been fiddling with most recently, I feel like it's the closest...



def getSysID(SMP_ID):
if SMP_ID is None:
return None
else:
SysID = SMP_ID.split('-')[0] + '-' + SMP_ID.split('-')[1]
return SysID


I also tried this method:



id_vals = !SMP_ID!.split("-")

SYS_ID = id_vals[0] + "-" + id_vals[1]

remainder = id_vals[2]


Something isn't working right, though! There are a few IDs from a different source that don't follow this pattern and are just a string of numbers without hyphens, I'm wondering if that could be a part of the problem?



I'm looking for a solution that will ignore the non-hyphenated IDs and nulls, and copy the truncated hyphenated IDs into a new field.










share|improve this question





























    1















    I have a shapefile with polygons and each polygon has an ID number that follows a pattern like this:



    SMP_ID
    1077-1-1
    8-2-2
    1281-1-3
    304-2-1


    In have created a new field and I need to populate it with a truncated version of these IDs, which would look like this:



    SYS_ID
    1077-1
    8-2
    1281-1
    304-2


    This is the code I've been fiddling with most recently, I feel like it's the closest...



    def getSysID(SMP_ID):
    if SMP_ID is None:
    return None
    else:
    SysID = SMP_ID.split('-')[0] + '-' + SMP_ID.split('-')[1]
    return SysID


    I also tried this method:



    id_vals = !SMP_ID!.split("-")

    SYS_ID = id_vals[0] + "-" + id_vals[1]

    remainder = id_vals[2]


    Something isn't working right, though! There are a few IDs from a different source that don't follow this pattern and are just a string of numbers without hyphens, I'm wondering if that could be a part of the problem?



    I'm looking for a solution that will ignore the non-hyphenated IDs and nulls, and copy the truncated hyphenated IDs into a new field.










    share|improve this question



























      1












      1








      1








      I have a shapefile with polygons and each polygon has an ID number that follows a pattern like this:



      SMP_ID
      1077-1-1
      8-2-2
      1281-1-3
      304-2-1


      In have created a new field and I need to populate it with a truncated version of these IDs, which would look like this:



      SYS_ID
      1077-1
      8-2
      1281-1
      304-2


      This is the code I've been fiddling with most recently, I feel like it's the closest...



      def getSysID(SMP_ID):
      if SMP_ID is None:
      return None
      else:
      SysID = SMP_ID.split('-')[0] + '-' + SMP_ID.split('-')[1]
      return SysID


      I also tried this method:



      id_vals = !SMP_ID!.split("-")

      SYS_ID = id_vals[0] + "-" + id_vals[1]

      remainder = id_vals[2]


      Something isn't working right, though! There are a few IDs from a different source that don't follow this pattern and are just a string of numbers without hyphens, I'm wondering if that could be a part of the problem?



      I'm looking for a solution that will ignore the non-hyphenated IDs and nulls, and copy the truncated hyphenated IDs into a new field.










      share|improve this question
















      I have a shapefile with polygons and each polygon has an ID number that follows a pattern like this:



      SMP_ID
      1077-1-1
      8-2-2
      1281-1-3
      304-2-1


      In have created a new field and I need to populate it with a truncated version of these IDs, which would look like this:



      SYS_ID
      1077-1
      8-2
      1281-1
      304-2


      This is the code I've been fiddling with most recently, I feel like it's the closest...



      def getSysID(SMP_ID):
      if SMP_ID is None:
      return None
      else:
      SysID = SMP_ID.split('-')[0] + '-' + SMP_ID.split('-')[1]
      return SysID


      I also tried this method:



      id_vals = !SMP_ID!.split("-")

      SYS_ID = id_vals[0] + "-" + id_vals[1]

      remainder = id_vals[2]


      Something isn't working right, though! There are a few IDs from a different source that don't follow this pattern and are just a string of numbers without hyphens, I'm wondering if that could be a part of the problem?



      I'm looking for a solution that will ignore the non-hyphenated IDs and nulls, and copy the truncated hyphenated IDs into a new field.







      arcgis-desktop field-calculator python-parser arcgis-10.5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 11 mins ago









      PolyGeo

      53.6k1780240




      53.6k1780240










      asked 1 hour ago









      Shelby GuercioShelby Guercio

      1889




      1889






















          1 Answer
          1






          active

          oldest

          votes


















          2














          Using the built-in string method of 'find', you can identify whether there is a hyphen in the field and process accordingly. One reason you may have errors is that on non-hyphenated records, there is no item at index position 1 from the split.



          def getSysID(SMP_ID):
          if SMP_ID.find("-") == -1 or SMP_ID is None: #might be able to leave off the or clause here
          return None
          else:
          SysID = SMP_ID.split('-')[0] + '-' + SMP_ID.split('-')[1]
          return SysID





          share|improve this answer


























          • When I tried it with the "or" clause, it gave an error, but once I omitted it, it worked like a charm! Thank you for the help!

            – Shelby Guercio
            1 hour ago






          • 1





            Might be a format error for or, I'll check and update later. Glad that worked!

            – smiller
            1 hour ago






          • 1





            Ah, I forgot to capitalize None. Updated.

            – smiller
            43 mins ago











          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f313525%2fsplitting-id-number-along-hyphens-and-creating-new-field-with-only-parts-of-id-i%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Using the built-in string method of 'find', you can identify whether there is a hyphen in the field and process accordingly. One reason you may have errors is that on non-hyphenated records, there is no item at index position 1 from the split.



          def getSysID(SMP_ID):
          if SMP_ID.find("-") == -1 or SMP_ID is None: #might be able to leave off the or clause here
          return None
          else:
          SysID = SMP_ID.split('-')[0] + '-' + SMP_ID.split('-')[1]
          return SysID





          share|improve this answer


























          • When I tried it with the "or" clause, it gave an error, but once I omitted it, it worked like a charm! Thank you for the help!

            – Shelby Guercio
            1 hour ago






          • 1





            Might be a format error for or, I'll check and update later. Glad that worked!

            – smiller
            1 hour ago






          • 1





            Ah, I forgot to capitalize None. Updated.

            – smiller
            43 mins ago
















          2














          Using the built-in string method of 'find', you can identify whether there is a hyphen in the field and process accordingly. One reason you may have errors is that on non-hyphenated records, there is no item at index position 1 from the split.



          def getSysID(SMP_ID):
          if SMP_ID.find("-") == -1 or SMP_ID is None: #might be able to leave off the or clause here
          return None
          else:
          SysID = SMP_ID.split('-')[0] + '-' + SMP_ID.split('-')[1]
          return SysID





          share|improve this answer


























          • When I tried it with the "or" clause, it gave an error, but once I omitted it, it worked like a charm! Thank you for the help!

            – Shelby Guercio
            1 hour ago






          • 1





            Might be a format error for or, I'll check and update later. Glad that worked!

            – smiller
            1 hour ago






          • 1





            Ah, I forgot to capitalize None. Updated.

            – smiller
            43 mins ago














          2












          2








          2







          Using the built-in string method of 'find', you can identify whether there is a hyphen in the field and process accordingly. One reason you may have errors is that on non-hyphenated records, there is no item at index position 1 from the split.



          def getSysID(SMP_ID):
          if SMP_ID.find("-") == -1 or SMP_ID is None: #might be able to leave off the or clause here
          return None
          else:
          SysID = SMP_ID.split('-')[0] + '-' + SMP_ID.split('-')[1]
          return SysID





          share|improve this answer















          Using the built-in string method of 'find', you can identify whether there is a hyphen in the field and process accordingly. One reason you may have errors is that on non-hyphenated records, there is no item at index position 1 from the split.



          def getSysID(SMP_ID):
          if SMP_ID.find("-") == -1 or SMP_ID is None: #might be able to leave off the or clause here
          return None
          else:
          SysID = SMP_ID.split('-')[0] + '-' + SMP_ID.split('-')[1]
          return SysID






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 42 mins ago

























          answered 1 hour ago









          smillersmiller

          2,044217




          2,044217













          • When I tried it with the "or" clause, it gave an error, but once I omitted it, it worked like a charm! Thank you for the help!

            – Shelby Guercio
            1 hour ago






          • 1





            Might be a format error for or, I'll check and update later. Glad that worked!

            – smiller
            1 hour ago






          • 1





            Ah, I forgot to capitalize None. Updated.

            – smiller
            43 mins ago



















          • When I tried it with the "or" clause, it gave an error, but once I omitted it, it worked like a charm! Thank you for the help!

            – Shelby Guercio
            1 hour ago






          • 1





            Might be a format error for or, I'll check and update later. Glad that worked!

            – smiller
            1 hour ago






          • 1





            Ah, I forgot to capitalize None. Updated.

            – smiller
            43 mins ago

















          When I tried it with the "or" clause, it gave an error, but once I omitted it, it worked like a charm! Thank you for the help!

          – Shelby Guercio
          1 hour ago





          When I tried it with the "or" clause, it gave an error, but once I omitted it, it worked like a charm! Thank you for the help!

          – Shelby Guercio
          1 hour ago




          1




          1





          Might be a format error for or, I'll check and update later. Glad that worked!

          – smiller
          1 hour ago





          Might be a format error for or, I'll check and update later. Glad that worked!

          – smiller
          1 hour ago




          1




          1





          Ah, I forgot to capitalize None. Updated.

          – smiller
          43 mins ago





          Ah, I forgot to capitalize None. Updated.

          – smiller
          43 mins ago


















          draft saved

          draft discarded




















































          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%2f313525%2fsplitting-id-number-along-hyphens-and-creating-new-field-with-only-parts-of-id-i%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

          Щит и меч (фильм) Содержание Названия серий | Сюжет |...

          Венесуэла на летних Олимпийских играх 2000 Содержание Состав...

          Meter-Bus Содержание Параметры шины | Стандартизация |...