import arcpy

arcpy.env.overwriteOutput = True

points = r'C:\Users\Temitope\PycharmProjects\Scripts\DATA\ne_10m_populated_places_simple.shp'
countries = r'C:\Users\Temitope\PycharmProjects\Scripts\DATA\ne_50m_admin_0_countries.shp'
outpath = r'C:\Users\Temitope\PycharmProjects\Scripts\Output'

arcpy.MakeFeatureLayer_management(points, 'points_layer')

with arcpy.da.SearchCursor(countries, ['FID', 'SOVEREIGNT']) as country_cursor:
    for x in country_cursor:
        print x[1]
        arcpy.MakeFeatureLayer_management(countries, 'countries_layer', """ "FID" = {} """.format(x[0]))
        arcpy.SelectLayerByLocation_management('points_layer', 'WITHIN', 'countries_layer')
        formatted_output_name = x[1].replace('(', '_').replace(')', '_')
        arcpy.FeatureClassToFeatureClass_conversion('points_layer', outpath, 'cities_in_{}'.format(formatted_output_name))
        print 'Successfully converted {}'.format(formatted_output_name)

print 'Finished'


Successfully converted Senegal
Saudi Arabia
Successfully converted Saudi Arabia
    Traceback (most recent call last):
      File "C:/Users/Temitope/PycharmProjects/Scripts/gis_scripts/gis_script.py", line 17, in <module>
        arcpy.FeatureClassToFeatureClass_conversion('points_layer', outpath, 'cities_in_{}'.format(formatted_output_name))
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe3' in position 1: ordinal not in range(128)

    Process finished with exit code 1

Any ideas?

  • 3
    I think this may be a duplicate of gis.stackexchange.com/a/140112/115
    – PolyGeo
    Aug 7 '18 at 20:50
  • 2
    Please edit your question to include the complete output, including the output from the print x[1] line.
    – user2856
    Aug 8 '18 at 2:45
  • Try formatted_output_name = x[1].decode('latin-1').replace('(', '_').replace(')', '_')
    – user2856
    Aug 8 '18 at 3:04
  • Luke, i have tried it but still the issue occurs..
    – temitope
    Aug 8 '18 at 15:18

The reason this is failing is that you are using .format() with both regular str and a unicode str (the one returned by arcpy.da.searchcursor).

for example this fails in python 2:

myustring = unicode('ã', 'utf8')
print 'regular old string plus {}'.format(myustring)

but this would work:

myustring = unicode('ã', 'utf8')
print u'no longer a regular old string plus {}'.format(myustring)

so in your example, this line should fix your problem (changing the first str to unicode):

arcpy.FeatureClassToFeatureClass_conversion('points_layer', outpath, u'cities_in_{}'.format(formatted_output_name))

Switching to python 3 (with arcgisPro) would fix these headaches but of course that's not an option for everyone!

  • Thank you sir. But as i did the correction as followed, another problem arises again
    – temitope
    Aug 9 '18 at 16:07
  • Line 18, in <module> print 'successfully converted {}'.format (formatted_output_name) UnicodeEncodeError:' ascii' codec can't encode character u'\xe3' in position 1: ordinal not in range (128)
    – temitope
    Aug 9 '18 at 16:13
  • It's the same exact problem, just change the string to unicode: print u'Successfully converted {}'.format(formatted_output_name)
    – toslund
    Aug 9 '18 at 20:42
  • Thank you sir. It works but another situation arises.. Guinea-Bissau
    – temitope
    Aug 9 '18 at 21:41
  • arcpy. arcpy.FeatureClassToFeatureClass_conversion('points_layer', outpath, 'cities_in_{}'.format(formatted_output_name)) arcgisscripting. ExecuteError: ERROR 000354: the name contain invalid characters
    – temitope
    Aug 9 '18 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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