I am trying to add new text ID's to a field using a basic counter function within the field calculator. I have two questions here 1. does the field calculator always have to use a function or can it use straight code that's not in a function? 2. I keep running into error 00539 which I believe is a syntax error. When I run this in a regular IDE it works out fine but when i throw it into the field calculator I always get that same error.
How do i fix this?
Below is the code I've written.
def countfunction(input):
count=0
for N in range (1600,2360,5):
if N >=0:
print "TBL01_01172017_"+format(N)+"00.JPG"
count =count +1
input = countfunction(1)
print input
-
Take a look at cursors instead of field calculator they are much more flexible: pro.arcgis.com/en/pro-app/arcpy/data-access/…Bera– Bera2017年01月18日 14:00:45 +00:00Commented Jan 18, 2017 at 14:00
-
Welcome to GIS SE. Please cut/paste the code and format it with the code format button. That way, readers can access your code easier.Aaron– Aaron ♦2017年01月18日 14:01:10 +00:00Commented Jan 18, 2017 at 14:01
-
code has been fixeduser76984– user769842017年01月18日 14:14:31 +00:00Commented Jan 18, 2017 at 14:14
-
Could you expand on what you are trying to accomplish?Bera– Bera2017年01月18日 14:18:35 +00:00Commented Jan 18, 2017 at 14:18
-
So for each feature (/row in table) you want to add a ID-string to a field?Bera– Bera2017年01月18日 14:30:48 +00:00Commented Jan 18, 2017 at 14:30
2 Answers 2
An update cursor will provide more flexibility compared to the field calculator.
Try code below in the Python window. Change path+name to your feature class and the field name.
import arcpy
fc = r'C:\TEST.gdb\polygons123'
count=1600
with arcpy.da.UpdateCursor(fc,'FIELDNAME') as cursor:
for row in cursor:
row[0]='TBL01_01172017_{0}00.JPG'.format(count)
count+=5
cursor.updateRow(row)
-
the "r" before your file path that is the read function correct??user76984– user769842017年01月18日 15:52:48 +00:00Commented Jan 18, 2017 at 15:52
-
No it stands for 'raw'/ its a raw string for the path. used instead of using '\\'NULL.Dude– NULL.Dude2017年01月18日 15:57:28 +00:00Commented Jan 18, 2017 at 15:57
This is just my two bits, the issue you are having is due to the double quotes in your string. One of the quirks of the field calculator is that you can't use double quotes for a string. I think you need to explain what you are trying to accomplish in more detail.
Few notes:
You are using the Python's string .format() function wrong. You can use key word arguments or index placeholders instead of tying to concatenate the string which totally defeats the purpose of using .format function in the first place.
keyword arguments:
"This string is {text} don't you think?".format(text="sweeeeeeeet")
index placeholder:
"This is {0} don't you {1}?".format("sweet","think")
Also, why are you using the "
if N >= 0
" when N will always be greater than 0 with the range you are using?Why have the input when your not using it?
- When incrementing by one you can just use the
variableInt += 1
instead ofvariableInt = variableInt + 1
Also, what is the point of the count variable in the first place? you arnt using it for anything since it is auto incremented with the range function in the for loop. You could simply reduce your code to the following if you just wanted to get it to work:
def countFunction():
for n in range (1600,2360,5):
return 'TBL01_01172017_{0}00.JPG'.format(n)
countfunction()
With all that said BERA's suggestion to use the Update Cursor in the Python Window enter image description hereof ArcMap might be a better option. If you were to run the code in field calculator you will get all the same values since you are performing the same function on all the rows instead of iterating thorough each row and calculating it individually. Hence in BERAs answer the part which says "for row in rows
".
Explore related questions
See similar questions with these tags.