1

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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 18, 2017 at 13:52
6
  • Take a look at cursors instead of field calculator they are much more flexible: pro.arcgis.com/en/pro-app/arcpy/data-access/… Commented 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. Commented Jan 18, 2017 at 14:01
  • code has been fixed Commented Jan 18, 2017 at 14:14
  • Could you expand on what you are trying to accomplish? Commented Jan 18, 2017 at 14:18
  • So for each feature (/row in table) you want to add a ID-string to a field? Commented Jan 18, 2017 at 14:30

2 Answers 2

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)

enter image description here

answered Jan 18, 2017 at 14:42
2
  • the "r" before your file path that is the read function correct?? Commented Jan 18, 2017 at 15:52
  • No it stands for 'raw'/ its a raw string for the path. used instead of using '\\' Commented Jan 18, 2017 at 15:57
0

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 of variableInt = 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".

answered Jan 18, 2017 at 15:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.