Problem in Field Calculator

2911
2
07-13-2015 03:09 AM
H_A_D_Padmasiri
New Contributor

Dear Sir!

      I tried to customize a field value in polygon featureclass. For that I wrote a python function. Then using Field Calculator, I copy this function to Pre_Logic Script Code: and call the function. it gives me error.

  Then I tried the same using Calculate Field tool in ArcToolbox. It works correctly.

Pl explain why I cannot run the above function using Field Calculator.

My function is given below:

def lblcal(a):

       Dic1 = {'11':'PP', '13':'TOPOPP', '14':'FTP', '15':'VP', '16':'FVP', '17':'CP', '18':'FCP', '19':'FSP', '20':'FSPP', '21':'ISPP', '22':'FUP'}

       if a[2:4] in Dic1.keys():

            m = Dic1.get(a[2:4])

            if (a[9] == 's') is True:

            str1 = 'SUP' + str(int(a[10:15]))

           else:

            str1 = 'Inset' + str(int(a[10:15]))

  if (a[15] == 's') is True:

  str2 = 'Sheet' + str(int(a[16:20]))

  str3 = m + ' ' + str(int(a[4:9])) + str1 + str2

  return (str3)

I am working in ArcGIS 9.3

Thanks

Padmasiri

Tags (1)
0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

you have to supply it with a field name as a substitute for 'a'

the function should go in the code block section then the call to the is made in the expression box like

lblcal([yourfield]) if using VB or  lblcal(!yourfield!) if using the Python parser

XanderBakker
Esri Esteemed Contributor

In addition to what Dan mentions, you should try and post the post using the instructions found here: Posting Code blocks in the new GeoNet

I copied the code and pasted it in an Python IDE and there were issues with the indentation. Apart from that you might be running into an error like "UnboundLocalError: local variable 'str3' referenced before assignment", since str3 only gets assigned in case a[15] is 's'.

def lblcal(a):
    Dic1 = {'11':'PP', '13':'TOPOPP', '14':'FTP', '15':'VP', '16':'FVP', '17':'CP', '18':'FCP', '19':'FSP', '20':'FSPP', '21':'ISPP', '22':'FUP'}
    if a[2:4] in Dic1:
        m = Dic1[a[2:4]]
        if a[9] == 's':
            str1 = 'SUP' + str(int(a[10:15]))
        else:
            str1 = 'Inset' + str(int(a[10:15]))
    if a[15] == 's':
        str2 = 'Sheet' + str(int(a[16:20]))
        str3 = m + ' ' + str(int(a[4:9])) + str1 + str2
    return (str3)

This code will probably still generate the error you encounter. What is the error you receive? Does it calculate for some records the values and breaks? Is it possible to provide some values of 'a' to check if it works?

BTW:

  • Dic1.keys() can be replace by Dic1
  • Dic1.get(a[2:4]) can be replaced by Dic1[a[2:4]]
  • if (a[9] == 's'): is True can be replaced by if a[9] == 's':
  • if (a[15] == 's') is True: can be replaced by if a[15] == 's':
  • Include at line before line 3 something like: str3 = "Error with: {0}".format(a) to avoid the code to break
  • There is no error checking (include try expect to catch errors)