Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to use Python to split a string field into three new fields at the separator " - ". The field is in the format "a - b - c". So I initially had this in the field calculator for newFieldA:

!myField!.partition(" - ")[0]

This gave me the value a in newFieldA. Similarly, I calculated newFieldB and newFieldC. After a more experienced Python user took a look at it, this was the final result:

' ' if !myField! == 'NULL' else !myField!.split(' - ')[0]

The test for NULL was put in because there were some NULL values throwing errors. Each of the three fields were calculated using this code, just the index was changed (0, 1 or 2).

My problem now is, after changing spelling errors in myField, I tried to split the field again, using the exact same code which worked previously. It now fails to run, giving the error of "wrong field name or unbalanced quotation marks". I am definitely using the correct field, and the quotation marks are not unbalanced. Also, I literally copied and pasted this from the code I used previously, so I don't see why it would be failing now. I feel like I'm missing something really simple. Does anyone have any ideas?

**edit** I've placed the following in the codeblock

define splitField(value):
' ' if value Is None
else:
  value.split(' - ')[0]

and in the expression window

splitField(!myField!)

I am getting a syntax error on line 1, I have not been using Python long enough to see what it could possibly be, as that looks right to me. Does anyone have a suggestion to make the code better?

edit I'm working with crime data, so in the field SUBACTION I have "Policing - Theft - Fraud". I then created three fields MANDATE, CATEGORY, and OFFENCE. The end result should be "Policing" in MANDATE, "Theft" in CATEGORY and "Fraud" in OFFENCE. I changed the codeblock to

def splitField(value):
  ' ' if !SUBACTION! == 'NULL'
      else:
        value.split(' - ')[0]

It is now giving a syntax error in line 2.

share|improve this question
3  
Can you maybe specify what you are entering into the Expression field as well as what you are entering into the Code Block –  PolyGeo Mar 13 '12 at 6:55
    
I was just using the Expression field, not the Code Block. I will try defining a function as suggested by Jeff Berry. –  Arabella Mar 14 '12 at 14:02
    
Yep - Jeff's way is certainly how I would do it –  PolyGeo Mar 14 '12 at 22:36
1  
In your codeblock, Cindy, you should use def instead of define. Also, make sure everything under the definition is indented one level. The code beneath your else should be indented one level further. It might help us if you show us a small sample of the data you are working with, including a 'NULL' field so we can be certain we are providing the correct help. If the null value is a string, as you said, then you should be using the == 'NULL' test as you had previous. –  Nathanus Mar 22 '12 at 14:15
    
I am not sure if the solution is this simple, but it looks like you have extra single quote marks in the 2nd line which could be causing the syntax error. Here is what you have now: ' ' if !SUBACTION! == 'NULL' Try this: if !SUBACTION! == 'NULL' –  Get Spatial Mar 27 '12 at 15:11

3 Answers 3

if !myField! == 'NULL' will not test for a null value. That will test a for a string equal to 'NULL'. Use:

if !myField! is None:

To accomlish your task, define a python function in the pre-logic code block and use that function in the expression window. The following code sample pulls the first token from a '-' delimited string.

enter image description here

share|improve this answer
    
Sorry I wasn't clear on that point, there are no NULL values, but some have "NULL" as a string. –  Arabella Mar 14 '12 at 14:00

Please see related threads on GIS-Lab Discussion Forum

!InputField!.split(u"-")[0]
share|improve this answer

try this:

>>> string = "a-b-c"
>>> string
'a-b-c'
>>> string.split('-')
['a', 'b', 'c']
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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