Issue Returning Unicode in Python Function

2366
5
01-31-2013 01:38 PM
MikeMacRae
Occasional Contributor III
Hey everyone,

I'm trying to encode some unicode in a label expression using the python parser and defining a function. I'm geting a weird unicode value being returned and I can't figure out why.

In the following code:

def FindLabel ([Unicode_Value]):
  import locale
  deflang, defencoding = locale.getdefaultlocale()
  S = u'Libert{}'.format(u'[Unicode_Value]'.encode(defencoding))
  return S


Where the field [Unicode_Value] contains records that have unicoding strings for each record. In my case, they are all u'\u00e9' which is the code for é

Instead, it returns 'Libertesri__0'

This should return 'Liberté' as the encoding on my computer is set to 'cp1252' the US Standard. I can get this to work if I hard code the é unicode value into the function like this:

S = u'Libert{}'.format(u'\u00e9'.encode(defencoding))


but somehow I think the file geodatabase feature class attribute table is throwing some ESRI unicoding when I use the field in my function.

can anyone suggest how I can fix this?

Thanks,
Mike
Tags (2)
0 Kudos
5 Replies
ChrisFox3
Occasional Contributor III
This issue is you are placing the variable [STATE_NAME] inside single quotes, you will see the value esri__0 is returned if you did the following:

def FindLabel ([Unicode_Value]):
  return '[Unicode_Value]'
0 Kudos
MikeMacRae
Occasional Contributor III
This issue is you are placing the variable [STATE_NAME] inside single quotes, you will see the value esri__0 is returned if you did the following:

def FindLabel ([Unicode_Value]):
  return '[Unicode_Value]'


Hmmm...by [STATE_NAME] you meant my field of [Unicode_Value]?

And you're right, by using
return '[Unicode_Value]'
I do get esri__0 returning. That's not what I want. I want the é to return....
0 Kudos
ChrisFox3
Occasional Contributor III
Right, sorry that was the field name i was playing with on my side. Yea because you are surronding a variable name with quotes it is returning the string representation of the variable name (esri__0) rather than the string value ('\u00e9') referenced by the variable. The reason you see esri__0 is because we actually modify the function to replace the [fieldname] with esri__0 before the function is run.

So does this not work?

def FindLabel ([Unicode_Value]):
  import locale
  deflang, defencoding = locale.getdefaultlocale()
  S = u'Libert{}'.format([Unicode_Value].encode(defencoding))
  return S
0 Kudos
MikeMacRae
Occasional Contributor III
Hey Chris, thanks for the explanation about why I am getting esri__0. That totally makes sense. I think I was thinking too hard when I added in the quotes.

I've tried your modified script and I am getting
Libert'\u00e9'
returned. It doesn't seem to be encoding the values in my field. I've tried to modify the string in my field to the following

'\u00e9'

\u00e9

u'\u00e9'


None of which returns the coded value, just the string literal after 'Libert'. Is this working on your end? Do you actually get
Liberté
returning?

Thanks again for taking your time.

Mike
0 Kudos
ChrisFox3
Occasional Contributor III
No, I didn't get that to work. I have been playing around with this can you use the utf-8 representation of the character:

"\xc3\xa9"

When I use that in a field this label expression works:

def FindLabel ([Unicode_Value]):
  S = u'Libert{}'.format(unicode([Unicode_Value],"utf8"))
  return S
0 Kudos