Select to view content in your preferred language

Python script, SQL syntax within CalculateField question, missing quotes?

1071
5
12-02-2010 06:16 PM
BryceStath
Emerging Contributor
Here's my code, the problem is calling the string variable ScriptSub
I know this isn't complex but it's going to save me a ton of time if I can just get this right. 
I know that I'm probably just missing a double or single quote somewhere.  I really need this to work.  Thanks in advance. 

import sys
import string
import os
import arcgisscripting

gp = arcgisscripting.create(9.3)

# Set a default workspace
gp.workspace = "C:\working\Y2010\Franks\Plant"
# Set a toolbox
gp.toolbox = "management"

ScriptNm = sys.argv[0]
ScriptSub = os.path.basename(ScriptNm)

try:

    #Add fields
    gp.addfield ("\\FT_testing.gdb\FT_2010_P000\FT10_P180_Plant_test", "Depth_ft", "float", "5", "2")
    gp.addfield ("\\FT_testing.gdb\FT_2010_P000\FT10_P180_Plant_test", "Script", "text", "15")
      
except:
    # If an error occurs when running Addfield, print out the error message.
    print gp.GetMessages(2)

try:
    ############################
    # CALCULATIONS
    gp.CalculateField_management("\\FT_testing.gdb\FT_2010_P000\FT10_P180_Plant_test", "Depth_ft", "[ftbathy] * 3.2808 ", "VB")

    #This line is the problem                                                                   RIGHT HERE ???
    gp.CalculateField_management("\\FT_testing.gdb\FT_ 2010_P000\FT10_P180_Plant_test", "Script", 'ScriptSub', "VB", "")    

except:
    # If an error occurs when running Addfield, print out the error message.
    print gp.GetMessages(2)
try:

    print ScriptNm
    print ScriptSub

except:
    # If an error occurs when running Addfield, print out the error message.
    print gp.GetMessages(2)

Tags (2)
0 Kudos
5 Replies
RDHarles
Regular Contributor
I see a few problems:

1.) Change your backslashes to forward slashes.

2.) To get the script name (a variable) populated in a field, use single AND double quotes like this:

'"'+ScriptSub+'"'
0 Kudos
BryceStath
Emerging Contributor
Thanks.
But what are the + signs for?  I have not seen this anywhere in the documentation I've been looking at. 
Is there any documentation where I can see this?  That would help me understand this better. 
Also, I'm running 9.3.  Maybe it's time to upgrade??
0 Kudos
RDHarles
Regular Contributor
The + signs are used because ScriptSub is a variable and the Tool requires that there be quotes around that parameter.
I know, your ScriptSub value is already a text string, but that's what the tool requires for whatever reason.
If you simply put 'ScriptSub', it would literally put the word ScriptSub in every record, therefore the quotes around the variable.
0 Kudos
BryceStath
Emerging Contributor
rdharles,
I really appreciate your help.  I understand what you are saying about the quotes and the syntax.  
I've found a work around that is actually going to be much more efficient. 

Below is what's working for me, I'm going to build upon it.  It's a lot sleeker looking and more functional than what I was using, so it's even better than I originally had.
I've tried the exact syntax in my older scripts and I generate the same errors.  So I'm a bit at a loss for why that is.  It probably has something to do with the client I'm using, or something behind the scenes.  For now, this will work. 

I got the base part from ArcScripts, the link is
http://arcscripts.esri.com/details.asp?dbid=14534


#-----------------------------------------------------------------------------------------------------------
# File Name Insert
# Script written by Rocky Rudolph - April, 2006 - Channel Islands National Park, California
# Purpose: for creating a field called "FILENAME" and attaching the filename of the shapefile to each entry 
#   in the attribute table. Use with as many shapefiles within a specified directory.
#   Useful when picking apart shapefile entries and combining into a separate file to
#   maintain a breadcrumb trail of the original shapefile name.  
#
# Run file within a directory containing all the shapfiles needing modification
# Make sure FILENAME field doesn't already exist in any of the shapefiles
#
#-----------------------------------------------------------------------------------------------------------

#import relevant modules, create geoprocessing dispatch object
import win32com.client, sys, string, os

gp = win32com.client.Dispatch("esriGeoprocessing.gpDispatch.1")

# Remember to change this to wherever your shapefiles are stored
gp.workspace = "C:\\working\\Y2010\\Franks\\Plant\\FT_testing.gdb\\testing"

try:
    fcs = gp.ListFeatureClasses("*", "all")
    fcs.reset()
    fc = fcs.Next()

    Script = sys.argv[0]
    ScriptSub = os.path.basename(Script)

    print Script
    print ScriptSub
    

    while fc:
        # Create the new field
        gp.AddField_management (fc, "FILENAME", "text", "", "", "50")

        # Apply the filename to all entries       
        gp.CalculateField_management (fc, "FILENAME", '"' + ScriptSub + '"')
        fc = fcs.Next()

except:
 print gp.GetMessages ()
0 Kudos
by Anonymous User
Not applicable
You can also do something like

arcpy.CalculateField_management(featureclass, "SPACESEARCH", '"%s-%s%s" % (!FACILITY!,!FLOOR!,!ROOM!)', "PYTHON")

so,

gp.CalculateField_management (fc, "FILENAME", '"%s" % ScriptSub')

should also work
0 Kudos