简体   繁体   中英

How to print out strings with unicode escape characters correctly

I am reading strings from a file which contain embedded unicode escape sequences, é as an example. When I print the literal strings using print() , the encodings are translated by print into the correct character but if I get the strings from stdin and print them out, print doesn't convert the escape sequences into the unicode characters.

For example, when I use:

print ("Le Condamn\u00e9 \u00e0 mort") 

python correctly prints Le Condamné à mort however, if I get the same string from stdin I get: Le Condamné à mort

Does anyone know how I can get python to translate the escape sequences to the correct unicode characters? Also, why does print behave differently when you give it a string literal rather than a string variable?

The à is being stored as a Unicode number for python so that it is printed as a 'à'. When you get it from another file, it is completely in string form meaning it is then stored as a '\à' where every character is a string. A solution to this would be to identify where the '\à' is in the list and then replace it with the 'à'

Here is some code that will convert the '\à' in the string into the character its supposed to be.

def special_char_fix(string):
    string = list(string)
    for pl, char in enumerate(string):
        if char == '\\':
            val = ''.join([string[pl + k + 2] for k in range(4)])
            for k in range(5):
                string.pop(pl)
            string[pl] = str(chr(int(val, 16)))
    return ''.join(string)

I believe that you are looking for str.encode("string-escape") function

example code

s = "Le Condamn\u00e9 \u00e0 mor"
ra=s.encode('unicode_escape').decode()
print(ra)

output

Le Condamn\xe9 \xe0 mor

the image contains the code snippet with output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM