This is a follow-up question to a previous post. For some reason the following script in Arcpy does not want execute the command. Please see fields and script below. (All credit to Aaron for the script)
enter image description here
import arcpy, os
fc = 'Segments'
with arcpy.da.UpdateCursor(fc, ["Name", "Name_From", "Name_To", "OID@"]) as cursor:
for row in cursor:
if row[0] != None: # Make sure there are no None type data
# 1) Split strings by "&" and 2) remove leading/tailing white space
cleaned = [x.strip() for x in row[1].split("&")] # "Name_From" field
cleaned2 = [x.strip() for x in row[2].split("&")] # "Name_To" field
# Tackling the "Name_From" field
if row[0] in cleaned: # Make sure "Name" is in "Name_From" field
cleaned.remove(row[0]) # Remove "Name" from field
if len(cleaned) > 1:
new = ' & '.join(cleaned)
row[1] = new
elif len(cleaned) == 1:
row[1] = cleaned[0]
else:
print "There was a problem with OID %s" % row[3]
# Tackling the "Name_To" field
if row[0] in cleaned2: # Make sure "Name" is in "Name_To" field
cleaned2.remove(row[0]) # Remove "Name" from field
if len(cleaned2) > 1:
new2 = ' & '.join(cleaned2)
row[2] = new2
elif len(cleaned2) == 1:
row[2] = cleaned2[0]
else:
print "There was a problem with OID %s" % row[3]
cursor.updateRow(row)
asked Apr 17, 2015 at 18:09
-
In your original example you did not have quotation marks--but you do in this example--I suspect that is the issue. (gis.stackexchange.com/questions/135890/clean-up-attributes) You will need to handle how the script deals with quotes.Aaron– Aaron ♦2015年04月17日 18:51:02 +00:00Commented Apr 17, 2015 at 18:51
-
As a general rule, I'd recommend wrapping code like this in a try/except block. And as a general forum practice, if you're posting about an error, it's helpful to include the error.recurvata– recurvata2015年04月17日 19:00:55 +00:00Commented Apr 17, 2015 at 19:00
-
Yes this was generated by ArcMap by itself. I will try and find a way too remove the quotation marks. I suspected that this might be the problem. Thank you Aaron.Dean van den Heever– Dean van den Heever2015年04月17日 19:02:29 +00:00Commented Apr 17, 2015 at 19:02
1 Answer 1
You need to remove all of the quotes from your strings. Here is one approach:
# 1) Split strings by "&" and 2) remove leading/tailing white space
cleaned = [x.strip().replace("\"","") for x in row[1].split("&")] # "Name_From" field
cleaned2 = [x.strip().replace("\"","") for x in row[2].split("&")] # "Name_To" field
# Continue with script
answered Apr 17, 2015 at 19:16
-
1Thank you Aaron. This worked perfectly. I am still learning all there is to ArcPy and you have helped immensely.Dean van den Heever– Dean van den Heever2015年04月17日 19:36:14 +00:00Commented Apr 17, 2015 at 19:36
lang-py