Select to view content in your preferred language

Error exporting files with python

2215
10
Jump to solution
07-16-2014 06:12 AM
JohannesBierer
Frequent Contributor

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?

0 Kudos
1 Solution

Accepted Solutions
ElleryChan
Occasional Contributor

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.

View solution in original post

0 Kudos
10 Replies
HåvardMoe
Deactivated User

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...

ToddUlery
Regular Contributor

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.

0 Kudos
JohannesBierer
Frequent Contributor

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()

0 Kudos
HåvardMoe
Deactivated User

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.

0 Kudos
ToddUlery
Regular Contributor

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.

0 Kudos
JohannesBierer
Frequent Contributor

It this - last code line

print arcpy.GetMessages()

???

0 Kudos
ToddUlery
Regular Contributor

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.

0 Kudos
JohannesBierer
Frequent Contributor

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()

??? 🙂

0 Kudos
ElleryChan
Occasional Contributor

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.

0 Kudos