ascii codec error

484
6
07-16-2020 10:31 AM
by Anonymous User
Not applicable

I'm trying to fix some old code we use to identify high crash locations. Between this year and last year the code has stopped working.   I am getting this error.


Running script BelmontSelection...
Failed script BelmontSelection...

Traceback (most recent call last):
File "E:\Backup\CrashReports\2020\2020CrashDataFiles\Script\Belmont_Crash_Selection - Copy.py", line 88, in <module>
Field_Map_of_Join_Features = Lat + ' "' + Lat + '" true true false 16 Double 6 15 ,First,#,' + Output + "," + Lat + ",-1,-1;" + Long + " \"ODOT_LONGI\" true true false 16 Double 6 15 ,First,#," + Output + "," + Long + ",-1,-1;" + Street_on + ' "' + Street_on + '" true true false 254 Text 0 0 ,First,#,' + Output + "," + Street_on + ",-1,-1;" + Ref + ' "' + Ref + '" true true false 254 Text 0 0 ,First,#,' + Output + "," + Ref + ",-1,-1;Inj_Crash \"Inj_Crash\" true true false 4 Short 0 4 ,First,#," + Output + ",Inj_Crash,-1,-1;Fat_Crash \"Fat_Crash\" true true false 4 Short 0 4 ,First,#," + Output + ",Fat_Crash,-1,-1;BUFF_DIST \"BUFF_DIST\" true true false 0 Double 0 0 ,First,#," + Output + ",BUFF_DIST,-1,-1;Latitude \"Latitude\" true true false 16 Double 6 15 ,Mean,#," + Crash_Data + "," + Lat + ",-1,-1;Longitude \"Longitude\" true true false 16 Double 6 15 ,Mean,#," + Crash_Data + "," + Long + ",-1,-1;SumInjury \"SumInjury\" true true false 4 Short 0 4 ,Sum,#," + Crash_Data + ",Inj_Crash,-1,-1;SumFatal \"SumFatal\" true true false 4 Short 0 4 ,Sum,#," + Crash_Data + ",Fat_Crash,-1,-1"
UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 22: ordinal not in range(128)

Failed to execute (BelmontSelection).
Failed at Thu Jul 16 09:44:42 2020 (Elapsed Time: 19.90 seconds)

I have tried looking for and removing all non ascii characters in the data and the code but even after that and running the code from an old copy the error persists.  How can i change the code so the decode error doesn't occur.  It seems to be failing on line 88 where it tries to make the field map.

# ---------------------------------------------------------------------------
# Belmont_modelAll.py
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Script arguments
Crash_Data = arcpy.GetParameterAsText(0)

Buffer_distance = arcpy.GetParameterAsText(1)
if Buffer_distance == '#' or not Buffer_distance:
Buffer_distance = "0.05 Miles" # provide a default value if unspecified

Selected_shp = arcpy.GetParameterAsText(2)
if Selected_shp == '#' or not Selected_shp:
Selected_shp = "C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\\Selected.shp" # provide a default value if unspecified

Freq = arcpy.GetParameterAsText(3)
if Freq == '#' or not Freq:
Freq = 13 #provides default value
Inj = arcpy.GetParameterAsText(4)
if Inj == '#' or not Inj:
Inj = 9 #provides default value
Fatal = arcpy.GetParameterAsText(5)
if Fatal == '#' or not Fatal:
Fatal = 4 #provides default value

#Expression statement
Expression = '"Join_Count"' + " > " + str(Freq) + " OR " + '"SumInjury"' + " > " + str(Inj) + " OR " + '"SumFatal"' + " > " + str(Fatal)

#Have user input what the crash severity is called in the data table
crashSeverity = arcpy.GetParameterAsText(6)

# Process: Add Inj_Crash Field to Data Table
arcpy.AddField_management(Crash_Data, "Inj_Crash", "SHORT", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Calculate new field
# Inj_Crash will show a 1 for an injury, not fatal; and 0 for any others
#This field will be summed later to create and total number of injury crashes field
#For Belmont County records there is one field for crash severity
#1 is fatal, 2 is injury and 3 is PDO

#Codeblock for injury calculation
codeblock = """def calc(sev):
if sev == 2:
return 1
else:
return 0"""

calcexp = "calc( !" + crashSeverity +"!)"

#Process: Calculate field
arcpy.CalculateField_management(Crash_Data,"Inj_Crash",calcexp,"PYTHON_9.3", codeblock)

# Process: Add Fat_Crash Field to Data Table
arcpy.AddField_management(Crash_Data, "Fat_Crash", "SHORT", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

# Calculate new field
# Fat_Crash will show a 1 for a fatal crash; and 0 for any others
#This field will be summed later to create and total number of fatal crashes field
#Not necessary now, but if a crash ever had more than one fatal crash, it would be necessary

#Codeblock for fatality calculation
codeblock2 = """def calc(sev2):
if sev2 == 1:
return 1
else:
return 0"""

calcexp2 = "calc( !" + crashSeverity + "!)"

#Process: Calculate field
arcpy.CalculateField_management(Crash_Data,"Fat_Crash",calcexp2,"PYTHON_9.3", codeblock2)

# Local variables:
Output = "C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\\Model_Buffer.shp"
SpatialJoin_shp = "C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\\Joined.shp"

#User inputs fields that will be kept in final table
Long = arcpy.GetParameterAsText(7)
Lat = arcpy.GetParameterAsText(8)
Street_on = arcpy.GetParameterAsText(9)
Ref = arcpy.GetParameterAsText(10)

#Create field map of joined features, sum inj and fatal fields
Field_Map_of_Join_Features = Lat + ' "' + Lat + '" true true false 16 Double 6 15 ,First,#,' + Output + "," + Lat + ",-1,-1;" + Long + " \"ODOT_LONGI\" true true false 16 Double 6 15 ,First,#," + Output + "," + Long + ",-1,-1;" + Street_on + ' "' + Street_on + '" true true false 254 Text 0 0 ,First,#,' + Output + "," + Street_on + ",-1,-1;" + Ref + ' "' + Ref + '" true true false 254 Text 0 0 ,First,#,' + Output + "," + Ref + ",-1,-1;Inj_Crash \"Inj_Crash\" true true false 4 Short 0 4 ,First,#," + Output + ",Inj_Crash,-1,-1;Fat_Crash \"Fat_Crash\" true true false 4 Short 0 4 ,First,#," + Output + ",Fat_Crash,-1,-1;BUFF_DIST \"BUFF_DIST\" true true false 0 Double 0 0 ,First,#," + Output + ",BUFF_DIST,-1,-1;Latitude \"Latitude\" true true false 16 Double 6 15 ,Mean,#," + Crash_Data + "," + Lat + ",-1,-1;Longitude \"Longitude\" true true false 16 Double 6 15 ,Mean,#," + Crash_Data + "," + Long + ",-1,-1;SumInjury \"SumInjury\" true true false 4 Short 0 4 ,Sum,#," + Crash_Data + ",Inj_Crash,-1,-1;SumFatal \"SumFatal\" true true false 4 Short 0 4 ,Sum,#," + Crash_Data + ",Fat_Crash,-1,-1"

# Process: Buffer
arcpy.Buffer_analysis(Crash_Data, Output, Buffer_distance, "FULL", "ROUND", "NONE", "")

# Process: Spatial Join
arcpy.SpatialJoin_analysis(Output, Crash_Data, SpatialJoin_shp, "JOIN_ONE_TO_ONE", "KEEP_COMMON", Field_Map_of_Join_Features , "INTERSECT", "", "")

# Process: Select
arcpy.Select_analysis(SpatialJoin_shp, Selected_shp, Expression)

#Delete local variables
# Process: Delete
arcpy.Delete_management(Output, "ShapeFile")
arcpy.Delete_management(SpatialJoin_shp, "ShapeFile")
0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

You will need to format the code

/blogs/dan_patterson/2016/08/14/script-formatting 


... sort of retired...
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What version of Python were you running the code on last year, and what version now?  Did you switch from running ArcMap to Pro?

0 Kudos
JoeBorgione
MVP Emeritus

I have tried looking for and removing all non ascii characters in the data and the code but even after that and running the code from an old copy the error persists.

You'll make yourself crazy looking for the offending characters. See 

SQL dump to Excel 

and/or

https://community.esri.com/thread/256232-unicode-errors 

for some suggested fixes

That should just about do it....
0 Kudos
RandyBurton
MVP Alum

My first thoughts would be to examine lines 77-85.  I think the backslashes are causing your problem; they are used to escape certain characters.  I think the specific problem is occurring with the '\2020'.  Put a small r in front of your variables.

# Instead of:
# Local variables:
Output = "C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\\Model_Buffer.shp"
SpatialJoin_shp = "C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\\Joined.shp"


# Try:
# Local variables:
Output = r"C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\Model_Buffer.shp"
SpatialJoin_shp = r"C:\Users\Transportation\Desktop\2020CrashDataFiles\Script\Output Files\Joined.shp"
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The other variables in line 88 are from user input parameters in lines 9-10 and lines 77-85.  You may need to check the values the user entered if you are still having issues.

JoeBorgione
MVP Emeritus

You may need to check the values the user entered if you are still having issues

Words that spread terror in the database world: allowing a user to enter what ever they feel like.  My first exposure to codec errors came from an open text anything goes Survey 123 app on outdated Apple Tablets.  As I recall all the guys responding here helped me out of that jam...

That should just about do it....
0 Kudos
RandyBurton
MVP Alum

Some of the problems I've come across (with iPhones and Collector most frequently) involves auto-correct where an apostrophe or quote will be replaced with the "intelligent" version which can take you to unicode errors.