3

I have a model which creates feature classes based on the value in an attribute (using Iterate Feature Selection). It works fine as long the field values don't have spaces, punctuation marks, etc. but fails otherwise.

I know in a script I can use arcpy.ValidateTableName() to convert "C:\Big Kalzas!" into a name that can be saved, "C__Big_Kalzas_". How does one do the same in a model?

I tried Calculate Value with an expression as simple as arcpy.ValidateTableName("%Field Name%") do defining and calling a function, but it keeps evaluating to 1.

Model diagram, showing expression

1 Answer 1

3

It turns out that anything needing an import must be wrapped in a function:

validate("%Field Name%")

def validate(s):
    import arcpy
    return arcpy.ValidateTableName(s)

example

1
  • That's good to know and well explained.. +1 from me. I don't do much model builder as it's easier for me to do in python (now), normally I'd go searching for 'bad' characters and do multiple replace(BadChar,'_') statements. What does it return if the intended name exists? nothing wrong with the text but if the name already exists it can cause a script/model to fail (unless overwrite is on, but sometimes you don't want overwrite). Commented Jun 22, 2015 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.