2010-05-18 3 views
9

Je viens de connaître Python depuis quelques jours. Unicode semble être un problème avec Python.Afficher la chaîne d'échappement en Unicode en Python

i dispose d'un fichier texte stocke une chaîne de texte comme celui-ci

'\u0110\xe8n \u0111\u1ecf n\xfat giao th\xf4ng Ng\xe3 t\u01b0 L\xe1ng H\u1ea1' 

je peux lire le fichier et imprimer la chaîne sur mais il affiche de façon incorrecte. Comment puis-je imprimer à l'écran correctement comme suit:

"Đèn đỏ nút giao thông Ngã tư Láng Hạ" 

Merci à l'avance

+1

Par "imprimer la chaîne", voulez-vous dire à une console? Si oui, c'est probablement votre console qui est le problème - êtes-vous sûr qu'il supporte les caractères Unicode? –

Répondre

0

Essayez cette

>>> s=u"\u0110\xe8n \u0111\u1ecf n\xfat giao th\xf4ng Ng\xe3 t\u01b0 L\xe1ng H\u1ea1" 
>>> print s 
=> Đèn đỏ nút giao thông Ngã tư Láng Hạ 
8
>>> x=r'\u0110\xe8n \u0111\u1ecf n\xfat giao th\xf4ng Ng\xe3 t\u01b0 L\xe1ng H\u1ea1' 
>>> u=unicode(x, 'unicode-escape') 
>>> print u 
Đèn đỏ nút giao thông Ngã tư Láng Hạ 

Cela fonctionne dans un Mac, où Terminal.App correctement rend sys.stdout.encoding défini sur utf-8. Si votre plate-forme ne définit pas cet attribut correctement (ou pas du tout), vous devrez remplacer la dernière ligne avec

print u.decode('utf8') 

ou tout autre encodage de votre terminal/console utilise. Notez que dans la première ligne j'affecte une chaîne littérale brute de sorte que les "séquences d'échappement" ne soient pas développées - cela imite simplement ce qui se passerait si la chaîne x était en cours de lecture depuis un fichier (texte ou binaire) avec ce contenu littéral.

1

Il est utile de montrer un exemple simple avec du code et de sortir ce que vous avez explicitement essayé. Au cas où votre console ne supporte pas le vietnamien. Voici quelques options:

# A byte string with Unicode escapes as text. 
>>> x='\u0110\xe8n \u0111\u1ecf n\xfat giao th\xf4ng Ng\xe3 t\u01b0 L\xe1ng H\u1ea1' 

# Convert to Unicode string. 
>>> x=x.decode('unicode-escape') 
>>> x 
u'\u0110\xe8n \u0111\u1ecf n\xfat giao th\xf4ng Ng\xe3 t\u01b0 L\xe1ng H\u1ea1' 

# Try to print to my console: 
>>> print x 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "C:\dev\python\lib\encodings\cp437.py", line 12, in encode 
    return codecs.charmap_encode(input,errors,encoding_map) 
UnicodeEncodeError: 'charmap' codec can't encode character u'\u0110' in position 0: 
    character maps to <undefined> 

# My console's encoding is cp437. 
# Instead of the default strict error handling that throws exceptions, try: 
>>> print x.encode('cp437','replace') 
?èn ?? nút giao thông Ng? t? Láng H?  

# Six characters weren't supported. 
# Here's a way to write the text to a temp file and display it with another 
# program that supports the UTF-8 encoding: 
>>> import tempfile 
>>> f,name=tempfile.mkstemp() 
>>> import os 
>>> os.write(f,x.encode('utf8')) 
48 
>>> os.close(f) 
>>> os.system('notepad.exe '+name) 

Espérons que cela vous aide.

Questions connexes