Using the Field Calculator (python) to create Full Path Name for hyperlink

466
3
05-25-2012 11:14 AM
JamieNeils
New Contributor
I'm trying to use the field calculator to populate the field "Hyperlink1" with the full path name of the document.  I have a field called "DOC_NO" and if there is a document number in it, I would like the field "Hyperlink1" to fill in the path name with the document number.  If there is no text in "DOC_NO" I do not want any text in the "Hyperlink1" field.  But I can't seem to get it work right.  Can anyone take a look?

Pre-Logic Script Code:

def myfunction(docno) :
if len(!DOC_NO!) == 0 :
    return ''"
  else:
    return "V:\Eng\Land Management\Easements\Scanned Documents\" + !DOC_NO! + ".tif"

Hyperlink1=
myfunction( !DOC_NO! )
0 Kudos
3 Replies
DarrenWiens2
MVP Honored Contributor
Outside of your function, you're in ArcGIS world - so you correctly use "!DOC_NO!" to reference the field, which passes each value, then "docno", to the function one at a time. Inside the function, you're in Python world, so you use the variable "docno".
def myfunction(docno) :
   if len(docno)== 0 :
     return ""
   else:
     return "V:\Eng\Land Management\Easements\Scanned Documents\" + docno + ".tif"
 

edit: also, check your quotes after the first return - you've got two single quotes, then a double quote.
0 Kudos
BruceNielsen
Occasional Contributor III
If you are going to use backslashes in a string, you need to identify it as a raw string:
return r"V:\Eng\Land Management\Easements\Scanned Documents\" + docno + ".tif"

so Python doesn't try to interpret the next character as a control character.
0 Kudos
curtvprice
MVP Esteemed Contributor
Darren is correct; you can't use the "!field!" syntax inside the function. And Bruce too...

Here's my solution. This would work with either numeric or text fields.

I like to use only one "return" (for readability).

Pre-Logic Script Code:
def myfunction(docno):
  docno = str(docno).strip() # remove any trailing spaces
  if len(docno).strip() == 0:
    val = ""
  else:
    # the "r" is needed
    val = r"V:\Eng\Land Management\Easements\Scanned Documents\" + docno + ".tif"
  return val


Hyperlink1 =

myfunction( !DOC_NO! )
0 Kudos