In my attribute table I have a column named 'BYGN_STAT' which includes information about building status (RA, IG, FA etc.) and dates (YYYYMMDD).

I would like to split this column, and separate building status 'IG' with the following dates. The length of the dates varies from one date to many.

I need to find 'IG' and the following dates before the next to letters. In this case this is 'FA', but it could also be another combination of two letters.

In the figure below I would like to find and copy the values in the red rectangle to a new column. Is this possible?

enter image description here

  • 1
    Are you trying to use the Python Parser of the ArcGIS Field Calculator or an ArcPy cursor? – PolyGeo Sep 6 '17 at 11:40
  • 1
    If the number of indexes (characters) are always the same for each row (strictly formatted) then slicing would be the easiest. – NULL.Dude Sep 6 '17 at 12:37

If the string is structured in this way throughout the field, try using text slicing to return part of the string. See link for more information.

Using a dictionary, we can identify the text elements within the string that we need to find positions for. Then using the string.find() function, we can identify their indexes. Then use these to find the text in between each.

Try the following:

arcpy.AddField_management(fc, "New_Field", "TEXT")
d = {"RA" : 0, "IG": 0, "FA": 0}

with arcpy.da.UpdateCursor(fc, ["BYGN_STAT", "New_Field"]) as cursor:
    for row in cursor:
        string = str(row[0])
        d["RA"] = string.find("RA")
        d["IG"] = string.find("IG")
        d["FA"] = string.find("FA")
        ra = string[d["RA"]: d["IG"]-1]
        ig = string[d["IG"]: d["FA"]-1]
        fa = string[d["FA"]:]

        print ig
        row[1] = ig
  • IG = row[0][19:38] will not work as I don't know where in the field IG will be in each row. In this case IG is between 19:38, but IG could be anywhere in the field. – gobrandal Sep 6 '17 at 10:55
  • ive updated the code – MacroZED Sep 6 '17 at 11:07
  • Almost, but as I mention I don't know the length of the IG. The length of the dates varies from one date to many. I only know that it ends when a new letter appears or the field after is empty (shown in the first row in the example picture). – gobrandal Sep 6 '17 at 11:38
  • What are the letters that will always appear? A dictionary can be used to identify the indexes of these within a string and then calculate accordingly. – MacroZED Sep 6 '17 at 11:45
  • Nice! (I had to change Add.Field to AddField_management). To make it more universal/robust, it would have been great to implement to more functions: 1) Letters that can appear are: TB, RA, IG, FA, MP. A "check if exist"-test would be useful. 2) The order of these two letter combinations can differ from row to row. Since I don't know which two letter combination that is placed before and after IG It would have been useful to sort the dict after the "string.find" sequence. Then the code would be perfect – gobrandal Sep 7 '17 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.