title: Convert unicode string representation of emoji to unicode emoji in python
body: <p>I'm using Python2 on Spark (PySpark and Pandas) to analyze data about emoji usage. I have a string like <code>u'u+1f375'</code> or <code>u'u+1f618'</code> that I want to convert to <code></code> and <code></code> respectively. </p>
  
  <p>I've read several other SO posts and the <a href="https://docs.python.org/2.7/howto/unicode.html#the-unicode-type" rel="nofollow noreferrer">unicode HOWTO</a>, trying to grasp <code>encode</code> and <code>decode</code> to no avail.</p>
  
  <p>This didn't work: </p>
  
  <pre><code>decode_udf = udf(lambda x: x.decode('unicode-escape'))
  foo = emojis.withColumn('decoded_emoji', decode_udf(emojis.emoji))
  Result: decoded_emoji=u'u+1f618'
  </code></pre>
  
  <p>This ended up working on a one-off basis, but fails the moment I apply it to my RDD. </p>
  
  <pre><code>def rename_if_emoji(pattern):
    """rename the element name of dataframe with emoji"""
  
    if pattern.lower().startswith("u+"):
      emoji_string = ""
      EMOJI_PREFIX = "u+"
      for part_org in pattern.lower().split(" "):
        part = part_org.strip();
        if (part.startswith(EMOJI_PREFIX)):
          padding = "0" * (8 + len(EMOJI_PREFIX) - len(part)) 
          codepoint = '\U' + padding + part[len(EMOJI_PREFIX):]
          print("codepoint: " + codepoint)
          emoji_string += codepoint.decode('unicode-escape')
          print("emoji_string: " + emoji_string)
      return emoji_string
    else:
      return pattern
  
  rename_if_emoji_udf = udf(rename_if_emoji)
  </code></pre>
  
  <p>Error: <code>UnicodeEncodeError: 'ascii' codec can't encode character u'\U0001f618' in position 14: ordinal not in range(128)
  </code></p>