python-3.xunicodebackslash

How to display Unicode symbols of playing card in Python to the terminal?


Why doesn't this work? I remove an extra backslash in a data file, but the Unicode characters are not displayed in the console?

import re 
file = open("arrayOfCards",'r')
while True:
    next_line = file.readline()
    if not next_line:
        break;
    d = next_line.split(',')
    print (re.sub(r'\'','',d[2]))
file.close()

card = ['A','diamonds','\U0001F0C1','14']
print (type(card[2]), card[2])

file arrayOfCards:

A,diamonds,\U0001F0C1,14
A,clubs,\\U0001F0D1,14

I have next OUT:

[qw@qw project]$
\U0001F0C1
\\U0001F0D1
<class 'str'> 🃁

Please help, I can't solve the problem.


Solution

  • It is because the file is read without executing unicode escapes. You can execute them manually using eval like this: d[2] = eval("'" + d[2] + "'"). You have to add quotes to start and end of string to make it a string literal for evaluation. Final code would look like this:

    import re 
    file = open("arrayOfCards",'r')
    while True:
        next_line = file.readline()
        if not next_line:
            break;
        d = next_line.split(',')
        d[2] = eval("'" + d[2] + "'")
        print (re.sub(r'\'','',d[2]))
    file.close()
    
    card = ['A','diamonds','\U0001F0C1','14']
    print (type(card[2]), card[2])