Converting Java escape characters & emojis in a sentence with Python

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Converting Java escape characters & emojis in a sentence with Python



For example, if I was given the following string:


temp = "That looks great ud83dudc4c Good Job!"



How can I convert it so that it either shows the actual emoji (👌) or the textual meaning (OK HAND SIGN)?



Final result should like:


temp = "That looks great 👌 Good Job!"



or


temp = "That looks great :OK HAND SIGN: Good Job!"



I have tried using the emoji library in Python but it appears to only support Python escape characters. Is there any way to convert Java escape characters to Python escape characters?



Thanks in advance!




1 Answer
1



No need for external libraries, use the built-in str function str.decode()


str


print temp.decode('unicode-escape')



or tell python that your string contains unicode characters by making it a string literal prepending it with u''


u''


temp = u'That looks great ud83dudc4c Good Job!'



A prefix of "u" or "U" makes the string a Unicode string



http://docs.python.org/2.5/ref/strings.html





I have tried doing what you mentioned, but I receive an error: AttributeError: 'str' object has no attribute 'decode'.
– Burhan Nurdin
9 hours ago





If you're on python3.x+ use bytes(temp).decode('unicode-escape')
– davedwards
9 hours ago


bytes(temp).decode('unicode-escape')






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results