I wrote a python script that copies image raster files selected through a defined polygon to another folder.
It works quite well with few tiles, but with a larger amount I get the following message?
"UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 4: ordinal not in range(128)"
What does this mean?
Solved! Go to Solution.
You maybe have solved your problem by now, but if not, here is a suggestion:
Since you are getting the error in the except clause, maybe the root cause is really the problem, and you need to find out where the exception is actually being raised. You could temporarily remove the try: and except: to expose where the exception is really occurring.
It means that you have some special characters in there somewhere (u'\xfc' is 'ü', I believe) that need to be handled as unicode. Standard string operators in python doesn't handle unicode. It's hard to be more specific without any code or data...
The simple answer is encoding conflict. The current file has some funky foreign Unicode characters.
Parsing the file is great, but encoding it back out in standard 'ascii' codec it will not recognize the funky foreign character, because it doesn't fit within the range of 128 code points.
Thank you for the answers.
What I didn`t get is that it works for 194 files (wold files inculded) and stops then with the error message?
I think there's no funky foreign character?
The code:
# Import arcpy module
import arcpy, os, sys, shutil, glob
from arcpy import env
arcpy.env.overwriteOutput = True
# Clip Feature definieren
AuswahlFeature = arcpy.GetParameterAsText(0)
StrOutPath = arcpy.GetParameterAsText(1)
temp_export_raster = StrOutPath
export_raster = StrOutPath
Ortho_Blattschnitt = "U:\\ArcUISTools15\\Data\\UIS_RIPS.gdb\\UIS_0100000017200001"
Blattschnitt = "Blattschnitt"
Auswahl_Layer = "Export_Fläche_L"
SelektionsLayer = "Blattschnitt"
# Output shape definieren
Orthos_Nummern_shp = StrOutPath + "\\Orthos_Nummern.shp"
FeldHinzugefuegt = StrOutPath + "\\Orthos_Nummern.dbf"
# AuswahlFeature = sys.argv[0]
print AuswahlFeature
arcpy.AddMessage(AuswahlFeature)
try:
# Process: Feature-Layer erstellen
arcpy.MakeFeatureLayer_management(Ortho_Blattschnitt, Blattschnitt, "\"KB\" >= 'L6316'", "", "OBJECTID OBJECTID VISIBLE NONE;SHAPE SHAPE VISIBLE NONE;FFC FFC VISIBLE NONE;OAC OAC VISIBLE NONE;GEOM_ID GEOM_ID VISIBLE NONE;OBJECT_ID OBJECT_ID VISIBLE NONE;KB KB VISIBLE NONE;STAND STAND VISIBLE NONE;BILDFLUG BILDFLUG VISIBLE NONE;OBJECT_ID_1 OBJECT_ID_1 VISIBLE NONE;KB_1 KB_1 VISIBLE NONE;STAND_1 STAND_1 VISIBLE NONE;SHAPE_Length SHAPE_Length VISIBLE NONE;SHAPE_Area SHAPE_Area VISIBLE NONE")
# Process: Feature-Layer erstellen (2)
arcpy.MakeFeatureLayer_management(AuswahlFeature, Auswahl_Layer, "", "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;Id Id VISIBLE NONE")
# Process: Layer lagebezogen auswählen
arcpy.SelectLayerByLocation_management(Blattschnitt, "intersect", Auswahl_Layer, "", "NEW_SELECTION") # "have_their_center_in"
# Process: Features kopieren
arcpy.CopyFeatures_management(Blattschnitt, Orthos_Nummern_shp, "", "0", "0", "0")
# Process: Feld hinzufügen
arcpy.AddField_management(Orthos_Nummern_shp, "Pfad", "TEXT", "", "", "200", "", "NON_NULLABLE", "NON_REQUIRED", "")
# Process: Feld berechnen
arcpy.CalculateField_management(FeldHinzugefuegt, "Pfad", "\"U:\\rips\\images\\dop_color\" & \"\\\" & [KB] & \"\\\" & [OBJECT_ID]& \".*\"", "VB", "")
# Process: Table to dBASE (multiple)
arcpy.TableToDBASE_conversion(FeldHinzugefuegt, export_raster)
# Prints the tiles
rows = arcpy.SearchCursor(Orthos_Nummern_shp)
row = rows.next()
while row:
print row.Pfad
# arcpy.CopyRaster_management(row.Pfad,temp_export_raster,"DEFAULTS","","","","","")
for file in glob.glob(row.Pfad):
shutil.copy(file, temp_export_raster)
row = rows.next()
del Blattschnitt, Auswahl_Layer, SelektionsLayer
# ggf kann noch ein Raster Catalog erstellt werden
except:
# If an error occurred while running a tool print the messages
print arcpy.GetMessages()
The error messag eshould tell you which line of the code it crashes on - that should give you a hint to where the culprit character comes into the picture.
Of course it is just a guess without having the raster images. I wonder if these rasters were made with some other software not ArcGIS. I would guess that if they were created using ArcGIS that it might not throw this issue.
Something is encoded with a different codec.
It this - last code line
print arcpy.GetMessages()
???
Johannes Bierer wrote:
I wrote a python script that copies image raster files selected through a defined polygon to another folder.
It works quite well with few tiles, but with a larger amount I get the following message?
"UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 4: ordinal not in range(128)"
What does this mean?
This is the error. But, does it provide you with the line number for where the crash occurs? Traceback.
We can see the character that is not capable of passing through. There may be more than this.
line 85, in <module>
print arcpy.GetMessages()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 4: ordinal not in range(128)
and in line 85 is written:
print arcpy.GetMessages()
??? 🙂
You maybe have solved your problem by now, but if not, here is a suggestion:
Since you are getting the error in the except clause, maybe the root cause is really the problem, and you need to find out where the exception is actually being raised. You could temporarily remove the try: and except: to expose where the exception is really occurring.