Select to view content in your preferred language

python field calculator issue

5115
8
01-27-2011 06:40 AM
by Anonymous User
Not applicable
Original User: frankv3

i am having trouble calculating a string field named Plat_Link in a polygon feature class that has an integer field named MAP_BOOK and an integer field name PAGE
i tested this code in IDLE and it worked fine if MAP_BOOK and PAGE were set (see picture), but it will not work in the field calculator, an error of 000989 is returned
the code was tried both of these ways (first way in attached picture):

MapNum = str(!MAP_BOOK!)
PageNum = str(!PAGE!)
if len(PageNum) == 1:
PlatLink = "\\\\" + "Covsdesrv" + '\\' + "GIS Common" + '\\' + "Plats" + '\\' + "00" + MapNum + '\\' + "00" + MapNum + "000" + PageNum + ".TIF"
elif len(PageNum) == 2:
PlatLink = "\\\\" + "Covsdesrv" + '\\' + "GIS Common" + '\\' + "Plats" + '\\' + "00" + MapNum + '\\' + "00" + MapNum + "00" + PageNum + ".TIF"
elif len(PageNum) == 3:
PlatLink = "\\\\" + "Covsdesrv" + '\\' + "GIS Common" + '\\' + "Plats" + '\\' + "00" + MapNum + '\\' + "00" + MapNum + "0" + PageNum + ".TIF"

AND

if (!PAGE!) = 1:
PlatLink = "\\\\" + "Covsdesrv" + '\\' + "GIS Common" + '\\' + "Plats" + '\\' + "00" + str(!MAP_BOOK!) + '\\' + "00" + str(!MAP_BOOK!) + "000" + str(!PAGE!) + ".TIF"
elif (!PAGE!) = 2:
PlatLink = "\\\\" + "Covsdesrv" + '\\' + "GIS Common" + '\\' + "Plats" + '\\' + "00" + str(!MAP_BOOK!) + '\\' + "00" + str(!MAP_BOOK!) + "00" + str(!PAGE!) + ".TIF"
elif (!PAGE!) = 2:
PlatLink = "\\\\" + "Covsdesrv" + '\\' + "GIS Common" + '\\' + "Plats" + '\\' + "00" + str(!MAP_BOOK!) + '\\' + "00" + str(!MAP_BOOK!) + "0" + str(!PAGE!) + ".TIF"

any help appreciated
0 Kudos
8 Replies
ChrisMathers
Deactivated User
Im not sure why that isnt working but for starters you should try string substitution instead of concatentation and raw strings so you dont have to excape all those'\'.

http://docs.python.org/release/2.6.6/library/stdtypes.html#string-formatting-operations

PlatLink = r"\\Covsdesrv\GIS Common\Plats\00%s\00%s000%s.TIF"%(MapNum,MapNum,PageNum)
0 Kudos
by Anonymous User
Not applicable
Original User: frankv3

Im not sure why that isnt working but for starters you should try string substitution instead of concatentation and raw strings so you dont have to excape all those'\'.

http://docs.python.org/release/2.6.6/library/stdtypes.html#string-formatting-operations

PlatLink = r"\\Covsdesrv\GIS Common\Plats\00%s\00%s000%s.TIF"%(MapNum,MapNum,PageNum)


thank you for the tip, i'm just learning to use this scripting language and any help is much appreciated
0 Kudos
by Anonymous User
Not applicable
Original User: frankv3

it is still not calculating in ArcMap, reports the Error 000989 - Python Syntax Error , this is the calculation code (saved out)

MapNum = str(!MAP_BOOK!)
PageNum = str(!PAGE!)
if len(PageNum) == 1:
PlatLink = r"\\Covsdesrv\GIS Common\Plats\00%s000%s.TIF"%(MapNum,MapNum,PageNum)
elif len(PageNum) == 2:
PlatLink = r"\\Covsdesrv\GIS Common\Plats\00%s00%s.TIF"%(MapNum,MapNum,PageNum)
elif len(PageNum) == 3:
PlatLink = r"\\Covsdesrv\GIS Common\Plats\00%s0%s.TIF"%(MapNum,MapNum,PageNum)
__esri_field_calculator_splitter__
PlatLink

it runs fine in the IDLE window as before, anyone know what i'm doing wrong?
0 Kudos
PatrickOMalley
Emerging Contributor
The code you are showing does not show indentation of the action parts of the if statements. Python is very picky about white space and there must be indentation. I'm certain when you typed it into IDLE it supplied the indentation for you automatically.

Try this

if len(PageNum) == 1:
    PlatLink = r"\\Covsdesrv\GIS Common\Plats\00%s000%s.TIF"%(MapNum,MapNum,PageNum )
elif len(PageNum) == 2:
    PlatLink = r"\\Covsdesrv\GIS Common\Plats\00%s00%s.TIF"%(MapNum,MapNum,PageNum)
elif len(PageNum) == 3:
    PlatLink = r"\\Covsdesrv\GIS Common\Plats\00%s0%s.TIF"%(MapNum,MapNum,PageNum)
0 Kudos
PatrickOMalley
Emerging Contributor
I know I put 4 spaces in front of every line that started with PlatLink, but they don't show up in my submittal to the forum. Maybe your indentation is fine and the forum software strips off excess whitespace.
0 Kudos
DanPatterson_Retired
MVP Emeritus
You have to put code in the code blocks in the html editor, select the # tool from the message posting toolbar, paste your code and the proper indentation will be maintained as in this nonsence example

names = ["Obi", "Bob", "Billy"]
for name in names:
  if name == "Obi":
    print name, "is ", True, "a", "Jedi master"
  else:
    print name, "is not a Jedi master"  

don't forget to copy, compile and run the script to see if it works 🙂
0 Kudos
by Anonymous User
Not applicable
Original User: frankv3

so, the field names go at the bottom in the Expression box and then the result goes above in the Code Block box
it's almost the opposite of how the VBA calculator expressions ran

this is what works for us to calculate hyperlinks based on two fields:
Code Block:
def SetLink(map,page):
if len(str(page)) == 1:
     num0 = "000"
elif len(str(page)) == 2:
     num0 = "00"
elif len(str(page)) == 3:
     num0 = "0"
return r"\\Covsdesrv\GIS Common\Plats\00%s\00%s%s%s.TIF"%( str(map), str(map), num0, str(page))

Expression:
SetLink( !MAP_BOOK!, !PAGE! )
0 Kudos
NiklasNorrthon
Frequent Contributor
Seems unecessarily complicated. Simpler version follows:
def SetLink(map, page):
    return r"\\Covsdesrv\GIS Common\Plats\%03d\%03d%04d.TIF % (map, map, page)
0 Kudos