Field Calculation: ERROR 000539

2350
8
11-01-2012 09:29 AM
LeoDonahue
Occasional Contributor III
I have been trying to do a field calculation in ArcMap 10.0 sp5 and PYTHON_9.3 expression type, with not much success.

The field I am trying to calculate is a Text field with a length of 50, even though the data in that field "should" never have a length longer than 12 (it's not my data), it's just the way that field was created.

The error:
Start Time: Thu Nov 01 10:13:08 2012
ERROR 000539: <type 'exceptions.SyntaxError'>: EOL while scanning string literal (<expression>, line 1)
Failed to execute (Calculate Field).
Failed at Thu Nov 01 10:18:15 2012 (Elapsed Time: 5 minutes 7 seconds)


There are some feature attribute values in the field that have some kind of padding applied to them which is causing this error - I believe.

.strip() is not removing the whitespace, as the script will process about 80% of the records before it hits the one record causing the problem.

Can anyone tell what I'm missing?
Tags (2)
0 Kudos
8 Replies
klem
by
New Contributor III
Check if are there any "\" characters in the string (like \n, \t). If so, replace them with "//" first.
klem
0 Kudos
BruceBacia
Occasional Contributor
It could be null values that are throwing it off.  For 'Expression' change it to

setAPN2(str(!PG_APN!).strip())



This will convert any null values to a string representation of 'None'.  Then alter your function like below  Even if this isn't what is causing your specific error, this is good practice to ensure against null values breaking your code.  You would then need to alter your function as below to only work on non-null values.  Also, you can wrap the whole thing in try and except blocks to analyze which values aren't calculating and why.  If none of the records get calculated, it's probably an error in the logic.  Can you post a screenshot of some of the sample record values in the PG_APN field? 

def setAPN2(parcel):
    if parcel != 'None':
        try:
            strValue = parcel[0:3] + '-' + parcel[3:5] + '-'
            if len(parcel) <= 8:
                strValue += parcel[-3:]
            else:
                strValue += parcel[-1:]
            return strValue
        execept:
            pass             
0 Kudos
BruceBacia
Occasional Contributor
Oops...missed part of the logic for parcel strings with length > 8.  Also added a calculation of error records to 'Error - Unable to calculate'

def setAPN2(parcel):
    if parcel != 'None':
        try:
            strValue = parcel[0:3] + '-' + parcel[3:5] + '-'
            if len(parcel) <= 8:
                strValue += parcel[-3:]
            else:
                strValue += parcel[5:8] + '-' + parcel[-1:]
        execept:
            strValue = 'Error - unable to calculate'
        return strValue
            
0 Kudos
BruceBacia
Occasional Contributor
Actually.....looking at the code and the error, i'm pretty sure what is wrong.  It is trying to strip the field name argument itself, which isn't a string.  That's why you're getting the scanning string literal error.  Try the solution below:

Expression
setAPN2(str(!PG_APN!))



Then alter the function:

def setAPN2(parcel):
    if parcel != 'None':
        try:
            parcel = parcel.strip()
            strValue = parcel[0:3] + '-' + parcel[3:5] + '-'
            if len(parcel) <= 8:
                strValue += parcel[-3:]
            else:
                strValue += parcel[5:8] + '-' + parcel[-1:]
        except:
            strValue = 'Error - unable to calculate'
        return strValue
0 Kudos
LeoDonahue
Occasional Contributor III
Bruce, Krzysztof,

Thanks for the tips.

I don't visually see a \n character in any of the values.  Does that mean it might be there anyway?

I am still getting this error when I run the following code.

[ATTACH=CONFIG]19106[/ATTACH]
0 Kudos
BruceBacia
Occasional Contributor
That's really weird.  I see no clear difference between records 959777 and 959778 that would cause one to calculate and the other to error out.  I would highly recommend the try and except blocks.  The calculation won't ever completely error out...if it can't perform the operation, it will default to 'Unable to calculate'.  This will allow you to analyze what is different about those records that aren't calculated. 

def setAPN2(parcel):
    try:
        parcel = parcel.strip()
        strValue = parcel[0:3] + '-' + parcel[3:5] + '-'  
        if len(parcel) <= 8:
            strValue += parcel[5:]
        else:
            strValue += parcel[5:8] + '-' + parcel[8:]
    except:
        strValue = 'Unable to calculate'
    return strValue
0 Kudos
LeoDonahue
Occasional Contributor III
If I start an edit session and put my cursor in the parcel record ending in 062, then use the arrow keys to arrow right, the value disappears.  I'm guessing this is why I should be trying to replace the \n values?
0 Kudos
klem
by
New Contributor III
Hi Leo,

you are probably right, there is "new line" (\n) or tab (\t) character, so first you should clean this field or add
parcel = parcel.replace("\n","")
parcel = parcel.replace("\t","")
...
lines to your code.
klem
0 Kudos