Select to view content in your preferred language

Python Code Assist!!!

730
3
Jump to solution
07-15-2013 02:59 PM
ShikoNjuno
Deactivated User
I'm trying to check for the existence of data within a particular attribute field in a given shapefile.
Below is the code I'm working with (on model builder) which I know needs major debbuging...but I'm not sure how.

Expression
x("%Taxlot%", "%Workspace%")

Code Block
def x(Taxlot, Workspace):
    import arcpy
    arcpy.env.workspace = Workspace
    if arcpy.Exists(Taxlot):
        return "true"
    else:
        return "false"

Data type
Boolean

The result I get when I run this piece is always "false", even when I think it's true :confused:

Note: "Taxlot" is the name of the model parameter which accepts a list of values from the user.
The code is supposed to check these input values against "Workspace", which is the name of the shapefile that holds the attribute table where the field of interest is.

This might be a little confusing but I can clarify for anyone that may have a suggestion or actual code to achieve this.
Thank you much!

Shiko
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
StacyRendall1
Frequent Contributor
First things first, you do not need this line in field calculator or calculate value codeblocks:
import arcpy


The problem is to do with your Taxlot variable, and there are two issues with it:

  1. you convert it to a string in the Expression, as it is wrapped in quotes ("). If it is supposed to be a string, that is fine, but in your case it should be kept as a list. See below.

  2. arcpy.Exists() checks that something like a feature class or shapefile exists, you cannot just pass it a list. If it is a list of feature class paths, I have given some corrected code below.

Expression
x(%Taxlot%, %Workspace%)


Note that I took the quotes away from both variables. Workspace is already a string, so it doesn't need the quotes. The only time you need quotes should be when you are changing types, i.e., if you had a number, but you wanted Python to deal with it as a string value.

Code block
def x(Taxlot, Workspace):   arcpy.env.workspace = Workspace   if all in [arcpy.Exists(t) for t in Taxlot]:     return True   else:     return False


This uses something called a list comprehension (a for statement within list brackets) that builds the list on the fly, it basically says if all the elements of the list exist, return True, otherwise return False.

View solution in original post

0 Kudos
3 Replies
StacyRendall1
Frequent Contributor
First things first, you do not need this line in field calculator or calculate value codeblocks:
import arcpy


The problem is to do with your Taxlot variable, and there are two issues with it:

  1. you convert it to a string in the Expression, as it is wrapped in quotes ("). If it is supposed to be a string, that is fine, but in your case it should be kept as a list. See below.

  2. arcpy.Exists() checks that something like a feature class or shapefile exists, you cannot just pass it a list. If it is a list of feature class paths, I have given some corrected code below.

Expression
x(%Taxlot%, %Workspace%)


Note that I took the quotes away from both variables. Workspace is already a string, so it doesn't need the quotes. The only time you need quotes should be when you are changing types, i.e., if you had a number, but you wanted Python to deal with it as a string value.

Code block
def x(Taxlot, Workspace):   arcpy.env.workspace = Workspace   if all in [arcpy.Exists(t) for t in Taxlot]:     return True   else:     return False


This uses something called a list comprehension (a for statement within list brackets) that builds the list on the fly, it basically says if all the elements of the list exist, return True, otherwise return False.
0 Kudos
ShikoNjuno
Deactivated User
Awesome!!!!
Thank you soooo much for taking the time to break it down like you did.
I do however have a question about the Exists function; what I want to do is process the list of taxlots that are in the file, rather than check if ALL list values are in the shapefile or not.This is what I'm getting from that piece of code.....is it possible to say
   if in [arcpy.Exists(t) for t in Taxlot]:
etc...??

Oh and just to clarify....taxlot is a user-input list of strings. So the type is specified as such.
Thanks Stacy.
0 Kudos
StacyRendall1
Frequent Contributor
Perhaps you should check the documentation here, it states that arcpy.Exists() takes "The name, path, or both of a feature class, table, dataset, layer, shapefile, workspace, or file to be checked for existence."

I still don't understand what Taxlot is. IF it is a list of paths to, say, shapefiles, then it will work. If you are trying to check if features within a shapefile/feature class exist, then this is not the right tool for you.

Can you provide an example of Taxlot?
0 Kudos