I'm new to Python scripting with ArcGIS for Desktop and I have unicode characters (arabic) in my attribute table.

I want to use these characters in my script but when I try to do that the script does not work.

How can I use unicode characters in my script?

import arcpy
arcpy.env.workspace = "c:/test"
listlyr = arcpy.mapping.ListLayers(mxd,"","")
# hatta agar chand dataframe ham dashte bashim baz ham select ra anjam mdahad
for a in listlyr:
    delim = arcpy.AddFieldDelimiters(a,"City")

    arcpy.SelectLayerByAttribute_management (a, "NEW_SELECTION", delim+"='u'\xd9\x86\xd9\x88\xd8\xb4\xd9\x87\xd8\xb1''")

u'\xd9\x86\xd9\x88\xd8\xb4\xd9\x87\xd8\xb1' means 'نوشهر'

  • Try using the corresponding unicode string in the script instead, something like u'\xe4. – Martin Mar 10 '14 at 15:39
  • Have a look at my answer here to get an idea how to start. – Martin Mar 10 '14 at 15:56
  • I couldn't solve my problem . i used unicode syntax lie u'\...' but it dosn't work. – BBG_GIS Mar 11 '14 at 16:33
  • Could you update your question with your current code? – Martin Mar 11 '14 at 17:20
  • I updated my question .please help me . thnx – BBG_GIS Mar 11 '14 at 17:55

your u'\xd9\x86\xd9\x88\xd8\xb4\xd9\x87\xd8\xb1' is utf-8.

instead use unicode. Also wrap the value with single quote.

u"City = '\u0646\u0648\u0634\u0647\u0631'"

and don't bother with delim. Should work without (if you're on recent (like 10.2 ish) release).


to understand unicode

watch Ned Batchelder's "Pragmatic Unicode, or, How do I stop the pain"

read Python docs on unicode

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.