Show escaped string as Unicode in Python -


I have known Python for a few days now, there might be a problem with Python in Unicode.

I have a text file that stores a text string like this

  '\ u0110 \ xe8n \ u0111 \ u1ecf n \ xfat giao th \ xf4ng Ng \ Xe3 t \ u01b0l \ xe1ng h \ u1ea1 ' 

I can read the file and print the string but it displays incorrectly.

  "Thanks in advance"  

How can I print it on the screen correctly:

>

  & gt; & Gt; & Gt; X = r '\ u0110 \ xe8n \ u0111 \ u1ecf n \ xfat giao th \ xf4ng Ng \ xe3 t \ u01b0 l \ xe1ng h \ u1ea1' & gt; & Gt; & Gt; U = Unicode (X, 'Unicode-escape') & gt; & Gt; & Gt; Print U Đèn đỏ nút giao thông Ngã tư Láng Hạ  

It works in a Mac where the terminal.app makes sys.stdout.encoding utf-8 . If your platform does not set that attribute correctly (or at all), then you must

  print u.codecode ('UTF8')  

or Any other encoding is using your terminal / console.

Note that in the first line, I assign a raw string literally so that the "escape sequence" should not be expanded - just applying a (text or binary) file with the basic content < What will happen if the code> x is being read.


Comments