arcpy: problem with coding

573
4
01-15-2023 11:15 PM
Labels (1)
GIS_Limburg_Province_Belgium
New Contributor

With arcpy I made a script to get the properties of the featureclasses and tables in Esri geodatabases (.gdb), and the properties of its fields. Simple script and it always has run well.
But now a fieldname happens to have the character € in it (fieldname = 'Budget_€') and the script crashes with the message 'UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 59: ordinal not in range(128)'.
Yes, a coding issue, and it shouldn't be that difficult to solve. But all I tried fails with the same message.

What I tried and crashes:

# -*- coding: utf-8 -*- (also tried # -*- coding: cp1252 -*-)
import codecs

< code >

for field in fieldLst:
f.write(gdbbasefolder+"\t"+gdbname+"\t"+table+"\t"+ field.name+"\t"+ field.type+"\t"+ str(field.length)+"\t"+shapeType +"\t"+str(factoryCode) +"\t"+hasZ +"\t"+hasM +"\t"+ hasSpatialIndex+"\t"+ str(recs))

in which 'field.name' gives the problems.

I also tried:

field.name.encode('utf-8')

field.name.decode('utf-8')

but it keeps crashing.

Using Python 2.7.14, running in IDLE 2.7.14.

Read several threads about this, but nothing helps.

What should I do?

0 Kudos
4 Replies
JohannesBierer
Occasional Contributor III

Don't know if it helps, but I tried to print the € in a field name like this?

 

import arcpy

featureclass = r"YourPath2FC"
field_names = [f.name.encode("windows-1252") for f in arcpy.ListFields(featureclass)]

print (field_names)

for f in field_names:
    print f.decode("windows-1252")

 

GIS_Limburg_Province_Belgium
New Contributor

Thanks for your reaction, but with Python 2 the script keeps crashing. As our organization is in the transition to ArcGIS Pro I ran my original script with Python 3 as well and now it worked! 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

This isn't an issue with ArcGIS Pro, so maybe it is time to switch?  Seriously though, the clock is ticking on ArcMap, and the clock long stopped for Python 2.

For Python 2 (I would not recommend for Python 3), you could consider changing the default standard output to UTF-8 at the start of your script:

 

import sys
import codecs
sys.stdout = codecs.getwriter("utf8")(sys.stdout)

 

Prior to Python 3.6, Python used the ANSI APIs to interact with the Windows OS, which usually meant cp437 for English and many other locales.  As you discovered, the euro symbol is not part of cp437, hence the error when trying to print or write from Python 2.7.14.  

GIS_Limburg_Province_Belgium
New Contributor

Thanks. You are right: switch to Pro, and we are already in transition. My original script runs anyway in Python 3.

0 Kudos