print python emoji as unicode string

前端 未结 6 1917
谎友^
谎友^ 2020-12-03 13:48

I\'ve been trying to output \"\" as \'\\U0001f604\' instead of the smiley but it doesn\'t seem to work.

i tried

相关标签:
6条回答
  • 2020-12-03 14:39

    Just add

    # -*- coding: UTF-8 -*-
    

    into your code and you will be able to print Unicode characters

    0 讨论(0)
  • 2020-12-03 14:39

    This code might help you to see how you can simply print an emoji:

    Code

    # -*- coding: UTF-8 -*-
    import re
    
    # string = 'Anything else that you wish to match, except URLs http://url.org'
    string = 'Anything else that you wish to match'
    matches = re.search(r'^(((?!http|https).)+)$', string)
    if matches:
        print(matches.group(1)+ " is a match                                                                     
    0 讨论(0)
  • 2020-12-03 14:41

    Another solution is to use the name aliases and print them using the string literal \N

    print('\N{grinning face with smiling eyes}')
    

    Current list of name aliases can be found at https://www.unicode.org/Public/14.0.0/ucd/NamesList-14.0.0d1.txt

    0 讨论(0)
  • 2020-12-03 14:41

    If this is for debugging purposes, you could use %r as the format specifier.

    >>> print '%r' % u'\U0001f604'
    u'\U0001f604'
    
    0 讨论(0)
  • 2020-12-03 14:49
    >>> print u'\U0001f604'.encode('unicode-escape')
    \U0001f604
    
    0 讨论(0)
  • 2020-12-03 14:51

    I found the solution to the problem.

    I wrote the following code:

    #convert to unicode
    teststring = unicode(teststring, 'utf-8')
    
    #encode it with string escape
    teststring = teststring.encode('unicode_escape')
    
    0 讨论(0)
提交回复
热议问题