I'm trying to slice with an index to get everything that comes after the first value in the MAIL_AD1 field and use the update cursor to put it in the Name_First field. As of now I can only get the second value and nothing more.
Here's the table I'm working with:
This puts the correct value in the Name_Last field:
fc = r"C:/pathto/fc"
field = ['MAIL_AD1', 'Name_First', 'Name_Last']
def Name_Last(): # this function updates the Name_Last field
with arcpy.da.UpdateCursor(fc, field) as cursor:
for row in cursor:
name = row[0] #get name in MAIL_AD1 list
if name is None: continue # continue to the next iteration in loop if None
s = str(unit).split() #split strings in MAIL_AD1 field
row[2] = s[0] #update Name_Last field with first value in MAIL_AD1 field
cursor.updateRow(row)
del cursor
But when I try to slice with an index number I get the error, which happens on the row[1] = s[1:] line.
def Name_First(): #this function updates the Name_First field
with arcpy.da.UpdateCursor(fc, field) as cursor:
for row in cursor:
unit = row[0] #get Unit in field list
if unit is None: continue # continue to the next iteration in loop if None
s = str(unit).split() #split strings in MAIL_AD1 field
row[1] = s[1:] #update Name_First field with what comes after first value in MAIL_AD1 field
cursor.updateRow(row)
del cursor
TypeError: value #1 - unsupported type: list
Solved! Go to Solution.
s = str(unit).split() # it returns list of ['Lovato', 'Dennis', 'J']
b = s[1:] # returns a list of ['Dennis', 'J']
# so in fact you must again convert list into string like this
c = " ".join(b) # it should retourn a string "Dennis J". you joined by space
#or
c = ", ".join(b) # should retourn "Dennis, J".
Advice: for this kinds of test run python directly in command line and test it. Or install Visual Studio code, install Python extention and run debugging. or test in arcgis pro notebook...
Regards, Tomasz
Your slicing retourns list (s[1:]) with one value, but you cannot insert it into row. Just replace s[1:] with s[1:][0]
@Tomasz_Tarchalski
If I run
row[1] = s[1:][0]
print(row[1])
it's only returning one value.
DENNIS
KYLE
LINDA
SUSAN
MICHAEL
NEILL
PAMELA
...
This is what I'm expecting:
DENNIS J
KYLE
LINDA KAY LVG TR
SUSAN K
MICHAEL JO ANN
NEILL DANIEL J
PAMELA
...
If I do this it's back in a list again.
row[1] = s[1:][0:]
print(row[1])
Prints:
['DENNIS', 'J']
['KYLE']
['LINDA', 'KAY', 'LVG', 'TR']
['SUSAN', 'K']
['MICHAEL', 'JO', 'ANN']
['NEILL', 'DANIEL', 'J']
['PAMELA']
...
s = str(unit).split() # it returns list of ['Lovato', 'Dennis', 'J']
b = s[1:] # returns a list of ['Dennis', 'J']
# so in fact you must again convert list into string like this
c = " ".join(b) # it should retourn a string "Dennis J". you joined by space
#or
c = ", ".join(b) # should retourn "Dennis, J".
Advice: for this kinds of test run python directly in command line and test it. Or install Visual Studio code, install Python extention and run debugging. or test in arcgis pro notebook...
Regards, Tomasz
Thanks @Tomasz_Tarchalski .
I was asleep at the wheel.