Using code block with Field Calculator error 000539

1310
5
Jump to solution
09-11-2021 04:38 PM
BenWilder
New Contributor II

Using ArcMap 10.8.1

The following Python script ran successfully about 3 weeks ago.

I wrote the script in Notepad. Then saved it from the Field Calculator. 

Load it today and I get: Error 000539: Runtime error Syntax Error: encoding declaration in Unicode string (<string>, line 0)

Notepad shows the file is encoded UTF-16 LE. I have read that Python uses UTF-8 by default. I have tried (based on suggestions on other forums) using a coding syntax like:

# _*_ coding: UTF-16 LE _*_ 

in first line of the Pre-Logic Script Code but that doesn't work and I don't really know what I am doing with that.

Seems like something basic is fouling the script up. Thanks for your suggestions. 

Attached: screen shot of the Field Calculator window.

Script:

def Blockname(Species):
if Species = "Olneya tesota":
return "PINV-OT"
elif  Species = "Parkinsonia florida":
return "PINV-PF
elif Species =  "Parkinsonia microphylla":
return "PINV-PM
else:
return "NA"
__esri_field_calculator_splitter__
Blockname(!Species!)

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - Esri Community

use Notepad ++ Notepad++ 8.1.4 release | Notepad++ (notepad-plus-plus.org)

you can set it to interpret and save a python encoded files

use UTF-8 or just dump the line period, but check the encoding to utf-8 in notepad


... sort of retired...

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - Esri Community

use Notepad ++ Notepad++ 8.1.4 release | Notepad++ (notepad-plus-plus.org)

you can set it to interpret and save a python encoded files

use UTF-8 or just dump the line period, but check the encoding to utf-8 in notepad


... sort of retired...
BenWilder
New Contributor II
Thank you, Dan!
I typically, have these files saved with the .cal extension so I can load them in Field Calculator.
I tried saving the file out of Notepad++ to a .cal file. That doesn't work.
I can copy the code block from either Notepad++ or Notepad and the script runs normally.
Thank you for pointing me in the right direction.
LukeAllen2
Occasional Contributor

Hi Ben, did you ever manage to save the expression as a .cal file? I ran into this same issue today, with a field calculator expression that I've been using for the last 2 years saved as a .cal file on my desktop. 

0 Kudos
BenWilder
New Contributor II
Hi Luke,
No, I have never been able to get saved .cal files to work. I have to copy and paste out of Notepad like Dan suggested.
Thanks for asking.
0 Kudos
LukeAllen2
Occasional Contributor

Hi, Ben (just in case you haven't already tried this) you may want to try pasting it in from notepad ++ again and just saving out a new .cal file, when I did this I no longer had issues, which made me think perhaps I unknowingly introduced something into the .cal file one time or saved it incorrectly, worth a shot if you haven't already tried. Seems to have worked for me.

 

Luke

0 Kudos