jdewar

Python issue in field calculator with quotes and apostrophes.

Discussion created by jdewar on Jul 19, 2011
Latest reply on Jul 22, 2011 by stacyrendall
I am stumped on this problem.  I am using the field calculator codeblock interface within model builder to automate some tasks.  As part of that, I have a simple if then statement that I use to remove any null or blank values from the select string fields.  The problem I am having is that if there are any double quotes AND apostrophes within the strings, the code will no work.  For example, the code below will work if there are apostrohes in the field strings, but not if there are double quotes.   

Expression: Reclass(str(!COM_NAME!))
Code block:
def Reclass(COM_NAME):
if COM_NAME == "" or COM_NAME == " ":
    return "[No Value Listed]"
else:
    return COM_NAME

On the other hand,  if I change the expression to Reclass(str('!COM_NAME!')), it will work with double quotes but not apostrophes.  VBS can get around this issue easily, but I am just curious if there is a simple work around in python as well.

Outcomes