Skip to main content
added 1200 characters in body
Source Link
Bera
  • 77.8k
  • 14
  • 78
  • 188

You are not showing all your code. But you could check if split list is to short, and if so then extend with empty string. Example:

a = '1st St & A blvd'
s = a.split(' & ')
print s

diff = 3-len(s)
if diff != 0:
    s.extend([' ']*diff)
    
print s

Outputs:

['1st St', 'A blvd']
['1st St', 'A blvd', ' ']

Or use try/except:

a = [1,2]
try:
    print a[2]
except IndexError: #a[2] is not possible, will give Indexerror, then dont do anything:
    pass

You could automate everything if you dont mind skipping field calculator:

import arcpy

fc = r'C:\GIS\ArcMap_default_folder\Default.gdb\ak_riks'
splitfield = ['KOMMUNNAMN']

rows = [i[0] for i in arcpy.da.SearchCursor(fc,splitfield)] #List all splitfield values
fields_needed = max([len(r.split('a')) for r in rows]) #Find out how many new fields are needed
prefix = 'Split_' #What to name new fields

newfields = []
for f in range(0,fields_needed):
    newfield = prefix+str(f)
    newfields.append(newfield)
    arcpy.AddField_management(in_table=fc, field_name=newfield, field_type='TEXT', field_length=20) #Adjust field length

with arcpy.da.UpdateCursor(fc, splitfield+newfields) as cursor:
    for row in cursor:
        newdata = row[0].split('a')
        diff = len(newfields) - len(newdata)
        if diff > 0:
            newdata = newdata+[' ']*diff
        else:
            newdata = newdata[:len(newfields)]
        row[1:] = newdata
        cursor.updateRow(row)

enter image description here

You are not showing all your code. But you could check if split list is to short, and if so then extend with empty string. Example:

a = '1st St & A blvd'
s = a.split(' & ')
print s

diff = 3-len(s)
if diff != 0:
    s.extend([' ']*diff)
    
print s

Outputs:

['1st St', 'A blvd']
['1st St', 'A blvd', ' ']

Or use try/except:

a = [1,2]
try:
    print a[2]
except IndexError: #a[2] is not possible, will give Indexerror, then dont do anything:
    pass

You are not showing all your code. But you could check if split list is to short, and if so then extend with empty string. Example:

a = '1st St & A blvd'
s = a.split(' & ')
print s

diff = 3-len(s)
if diff != 0:
    s.extend([' ']*diff)
    
print s

Outputs:

['1st St', 'A blvd']
['1st St', 'A blvd', ' ']

Or use try/except:

a = [1,2]
try:
    print a[2]
except IndexError: #a[2] is not possible, will give Indexerror, then dont do anything:
    pass

You could automate everything if you dont mind skipping field calculator:

import arcpy

fc = r'C:\GIS\ArcMap_default_folder\Default.gdb\ak_riks'
splitfield = ['KOMMUNNAMN']

rows = [i[0] for i in arcpy.da.SearchCursor(fc,splitfield)] #List all splitfield values
fields_needed = max([len(r.split('a')) for r in rows]) #Find out how many new fields are needed
prefix = 'Split_' #What to name new fields

newfields = []
for f in range(0,fields_needed):
    newfield = prefix+str(f)
    newfields.append(newfield)
    arcpy.AddField_management(in_table=fc, field_name=newfield, field_type='TEXT', field_length=20) #Adjust field length

with arcpy.da.UpdateCursor(fc, splitfield+newfields) as cursor:
    for row in cursor:
        newdata = row[0].split('a')
        diff = len(newfields) - len(newdata)
        if diff > 0:
            newdata = newdata+[' ']*diff
        else:
            newdata = newdata[:len(newfields)]
        row[1:] = newdata
        cursor.updateRow(row)

enter image description here

Source Link
Bera
  • 77.8k
  • 14
  • 78
  • 188

You are not showing all your code. But you could check if split list is to short, and if so then extend with empty string. Example:

a = '1st St & A blvd'
s = a.split(' & ')
print s

diff = 3-len(s)
if diff != 0:
    s.extend([' ']*diff)
    
print s

Outputs:

['1st St', 'A blvd']
['1st St', 'A blvd', ' ']

Or use try/except:

a = [1,2]
try:
    print a[2]
except IndexError: #a[2] is not possible, will give Indexerror, then dont do anything:
    pass