Select to view content in your preferred language

Python ArcGIS 10.1 Field Calculator If...Then Issue with None

2485
5
Jump to solution
07-03-2012 08:35 AM
Charles-AndreRoy
Regular Contributor
Hi all,

I created a simple 'Code Block' python script for the Field Calculator in ArcGIS 10. It was working fine then. Since I installed ArcGIS 10.1, something strange is happening when I run it. The purpose is to concatenate 3 different text fields into a new field as Provincial Code (2 numbers) + Trail Code (4 numbers) + Section Code (1 letter). Provincial Code and Trail codes don't have Null values but the Section code has Null value in it. So when a section code exist, the Project Code will look like 01-0001A but with look like 01-0001 if no section code exists. Here is the 'Code Block' I'm using:

Expression = classify(!ID_Province!, !ID_Trail!, str(!ProjSection!))  def classify(prov, trail, section):   if section == "":     return prov + "-" + trail   else:     return prov + "-" + trail + section


So when the Section code is Null or empty, I want to concatenate provincial and trail only (don't want any space or anything else after). Then, if a section code exist, I want to concatenate all of them together. As I said, it was working fine in ArcGIS 10. With ArcGIS 10.1, the code seems to only calculate the 'else' statement. If a section code exist, the code returns as expected something like 01-0001A. However, if section code is Null, instead of having something like 01-0001 I have 01-0001None. I don't understand why None appears now at the end.

Thanks for the help
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Esri Alum
Expression = classify(!ID_Province!, !ID_Trail!, !ProjSection!)

And

def classify(prov, trail, section):   if section:     return prov + "-" + trail   else:     return prov + "-" + trail + (str(section) if section else '')

View solution in original post

0 Kudos
5 Replies
NobbirAhmed
Esri Regular Contributor
Modify your codeblock slightly. Instead of testing a certain value ("") for section, use the generic statement whether section has some value. Your code should look like this:

def classify(prov, trail, section):
  if section:  # if section has any value
    return prov + "-" + trail + section
  else:    # if section is empty or null or None
    return prov + "-" + trail
0 Kudos
Charles-AndreRoy
Regular Contributor
Modify your codeblock slightly. Instead of testing a certain value ("") for section, use the generic statement whether section has some value. Your code should look like this:

def classify(prov, trail, section):
  if section:  # if section has any value
    return prov + "-" + trail + section
  else:    # if section is empty or null or None
    return prov + "-" + trail


Thanks Ahmed for your quick reply. However, I'm still facing the same issue...None appears at the end each time I have Null value as section code.
0 Kudos
JasonScheirer
Esri Alum
Expression = classify(!ID_Province!, !ID_Trail!, !ProjSection!)

And

def classify(prov, trail, section):   if section:     return prov + "-" + trail   else:     return prov + "-" + trail + (str(section) if section else '')
0 Kudos
Charles-AndreRoy
Regular Contributor
Expression = classify(!ID_Province!, !ID_Trail!, !ProjSection!)

And

def classify(prov, trail, section):
  if section:
    return prov + "-" + trail + section
  else:
    return prov + "-" + trail + (str(section) if section else '')


Thank you very much Jason. I don't really understand why I need to do this, but it's working now. I read somewhere that for Null values we had to put str in the expression and not in the code block. Can you explain the logic behind this?
0 Kudos
JasonScheirer
Esri Alum
Null values used to behave somewhat inconsistently; now they are correctly treated as the python value None across the system. You can do something like

if value is None


now in places to test if a value is Null in the table.
0 Kudos