1

Is it possible to split a field name within an attribute table of a feature class into two new fields where just the field name is being split. Please note that I do not need to split the features (rows), just the field name.

So for example, I have a field name called "JoinCount_5meters" and I would like to split this field name into two new fields called "JoinCount" and "5meters". Again, the features themselves (row values) do not need to be split. so, anyone have any ideas how I could split a field name into two new field names in ArcGIS?

I understand I can do this manually but I have 250 feature classes for which I need to split 16 field names in each feature class. It would be great if I can do this process using Python or within model builder using an iterator.

  • Do you want to add the fields, if so what are they called? How do you detect the field names to be split/calculated? I'm having trouble understanding the task. You want to add fields called JoinCount, 5metres and calculate them to JoinCount and 5metres.. is that right? It's not difficult but I need to understand how to find the field names to be split. – Michael Stimson Mar 2 '15 at 0:46
  • @MichaelMiles-Stimson Thank you so much for your help and comments. What I am essentially trying to do is split a string from a inline variable substiution in model builder. I have explained what I am trying t oaccomplish in detail in another post, here. – Jelle Mar 2 '15 at 6:53
  • @MichaelMiles-Stimson As I am adding this inline variable substitution to a field name I thought of a workaround by maybe splitting the field name as I tried to explain in this post here.After your answer below, I figured that it should also be possible to just split the string variable without working with the field names themselves. This should be a much easier solution I believe. If you still would like to help me and know how to, then I suggest you post in my other post. Do know that I will probably need to write a python script tool that I need to use within model builder. – Jelle Mar 2 '15 at 6:54
2

Assuming you want to split any field with an underscore you can use ListFileds (which returns Field Objects) with a wildcard to get the fields to split then string.split("_") to break it up into parts then calculate with quotes to populate the string:

import arcpy, os, sys

InFolder = sys.argv[1] # folder for shapefiles, or database for feature classes

arcpy.env.workspace = InFolder

for ThisFC in arcpy.ListFeatureClasses():
    # assuming any field with an underscore needs to be split
    SplitFields = arcpy.ListFields(ThisFC,"*_*")
    # iterate through each field found with an underscore
    for ThisField in SplitFields:
        # skip shape length and shape area fields
        if not ThisField.baseName.lower() == "shape_length" and not ThisField.baseName.lower() == "shape_area" :
            # break it up into parts
            Parts = ThisField.baseName.split("_")
            for ThisPart in Parts:
                # Add a field for each part
                arcpy.AddField_management(ThisFC,ThisPart,"TEXT",field_length = 30)
                # populate with the part ("\"" is a quote). In order to put a string into
                # a field it is necessary to quote it so the processor understands that it's
                # a string to be populated and not a field name..
                arcpy.CalculateField_management(ThisFC,ThisPart,"\"" + ThisPart + "\"","PYTHON")

Note, there's no error checking here, if a field already exists it will fall over. A try/catch on the addfield would fix that but in this case you'd possibly want to skip the whole bit... if you use ListFields with the full field name it can be used like a boolean:

DoesFieldExist = arcpy.ListFields("FullFieldName")
if not DoesFieldExist:

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.