Select to view content in your preferred language

Question about Python

1551
6
Jump to solution
02-06-2017 11:34 AM
BrentMiron
Emerging Contributor

I have a PYTHON script that works in 10.3 but will not work now in 10.4. Anyone know why?

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

The error message indicates a character encoding problem with the !SPCOMP! field.  You might try a decode to ascii.  Also, I think your indexing to get "90" in your example should be 2 and 5 not 3 and 6.

def getPJ(spcomb):
        if 'PJ' in spcomb:
                n = spcomb.find('PJ')
                return spcomb[n+2:n+5].strip()
        else: 
                return 0

getPJ( !SPCOMP!.decode('ascii') )
‍‍‍‍‍‍‍‍

View solution in original post

6 Replies
RandyBurton
MVP Alum

Can you share the script (at least the part that is generating the error) and the error message?

BrentMiron
Emerging Contributor

I am attempting to write a script where I have a field called SPCOMP containing info such as "PJ 90BW 10". I am trying to pull the numbers and put them in respective fields. E.G. A field called PJ would be populated with the number 90. I had it work once, but now I am receiving this error - ERROR 000539: Runtime error SyntaxError: encoding declaration in Unicode string (<string>, line0). Here is the code I am Using

def getPJ(spcomb):
if 'PJ' in spcomb:
n = spcomb.find('PJ')
return spcomb[n+3:n+6]
else:
return 0

PJ =

getPJ( !SPCOMP! )

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

trying reposting you code using https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet?s...‌   so we can rule out tabs/spacing as being the issue since Python is very particular about that.

Make sure if you had any special libraries/mods that you were using, that you copied those over to the new location for 10.4 (i.e. path). 

Also, how are you running this script?  As a custom tool, in the ArcGIS Desktop/Catalog python window, as a standalone exe, or in an external python IDE?  If anything but as a tool or Desktop, it may be a path issue.

0 Kudos
BrentMiron
Emerging Contributor

I am attempting to write a script where I have a field called SPCOMP containing info such as "PJ 90BW 10". I am trying to pull the numbers and put them in respective fields. E.G. A field called PJ would be populated with the number 90. I had it work once, but now I am receiving this error - ERROR 000539: Runtime error SyntaxError: encoding declaration in Unicode string (, line0). I am running this straight from the field calculator. Attached is the code

0 Kudos
RandyBurton
MVP Alum

The error message indicates a character encoding problem with the !SPCOMP! field.  You might try a decode to ascii.  Also, I think your indexing to get "90" in your example should be 2 and 5 not 3 and 6.

def getPJ(spcomb):
        if 'PJ' in spcomb:
                n = spcomb.find('PJ')
                return spcomb[n+2:n+5].strip()
        else: 
                return 0

getPJ( !SPCOMP!.decode('ascii') )
‍‍‍‍‍‍‍‍
BrentMiron
Emerging Contributor

Thank you very much this worked!

Brent

0 Kudos