Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a very simple script that I'm testing. It has always been working so far, but now I'm running in an unexpected error. When I try to save my files in a folder different than the environment one, it outputs this error:

    outSnowmask.save (newpath_01+ '\\'+ 'snow_'+ MODIS)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 47: ordinal not in range(128)

My code is:

import arcpy, os, sys, shutil

newpath_01= r'C:\\Users\\unimi\\Documents\\Umberto\\Università\\PhD\\Karakoram\\MODIS_Pakistan\\Aspect\\temporary_01'
if not os.path.exists (newpath_01):
    os.makedirs (newpath_01)

arcpy.env.workspace = 'C:\\Users\\unimi\\Documents\\Umberto\\Università\\PhD\\Karakoram\\MODIS_Pakistan\\MODIS_Clip\\Sample_test'
arcpy.env.overwriteOutput= True
from arcpy.sa import *

listMODIS = arcpy.ListRasters ()
print listMODIS
arcpy.CheckOutExtension ('Spatial')

for MODIS in listMODIS:
    outSnowmask = ExtractByAttributes(MODIS, "VALUE = 200")
    outSnowmask.save (newpath_01+ '\\'+ 'snow_'+ MODIS)
share|improve this question

1 Answer 1

Your problem lies in the use of the r prefix on the string in the variable newpath_01. Using this forces it to use a strict ASCII character set. The error shows that character position 47, does not fall in the 128 character ASCII range.

The à in Università, is not in the basic ASCII character set. When you assign the r prefix instead of just a regular string, it changes how the text string is interpreted.

This section of the python website, seems to describe why:

One syntactic restriction not indicated by these productions is that whitespace is not allowed between the stringprefix and the rest of the string literal. The source character set is defined by the encoding declaration; it is ASCII if no encoding declaration is given in the source file; see section 2.1.4.

I know this is for an old release, but I think it still applies.

You might try the following, instead:

newpath_01= 'C:\\Users\\unimi\\Documents\\Umberto\\Università\\PhD\\Karakoram\\MODIS_Pakistan\\Aspect\\temporary_01'

Since you have already escaped all of the backslashes in the file path, you should be able to get away with a standard string. Essentially, use the same string syntax as you did when setting the environment workspace.

share|improve this answer
    
Thank You. Indeed the problem was caused by that "à". Anyway, even getting rid of it didn't solve the problem. I found it simpler just to rename the folder (I know it's a drastic solution, if someone could suggest me how to read the path the correct way please tell me, I'll appreciate it a lot!) –  umbe1987 Sep 27 '13 at 10:46
    
@user9518 - I'm a bit confused. You say it solved the problem, but then say it didn't solve the problem. Would you please edit your original question with what you did? If you are still receiving an error message, especially if it is a new one, include that as well. –  Get Spatial Sep 27 '13 at 15:00
    
When I remove the "r" prefix it gives me the same error. When instead I rename the folder "Università" using a normal "a" instead of that "à", I point to this folder and the problem is solved. Again, it's not the fairest solution, but I'll avoid having further problems using this path in the future. –  umbe1987 Sep 30 '13 at 8:52
    
That makes me wonder if this is more a python issue then than specifically the type of string designator. If the original file path, including the character with the accent, works when you set the environment variable, but not when you use the python os module to verify the file path, it would seem there is a difference in the character set that ESRI is using to parse a string, and what python uses in general, to parse a string. As a general rule, it has always seemed that where windows is involved, basic is better with directory names, etc. No special characters and the like. –  Get Spatial Sep 30 '13 at 15:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.