问题
I want to make a dictionary where English words point to Russian and French translations.
How do I print out unicode characters in Python? Also, how do you store unicode chars in a variable?
回答1:
To include Unicode characters in your Python source code, you can use Unicode escape characters in the form \u0123
in your string, and prefix the string literal with 'u'.
Here's an example running in the Python interactive console:
>>> print u'\u0420\u043e\u0441\u0441\u0438\u044f'
Россия
Strings declared like this are Unicode-type variables, as described in the Python Unicode documentation.
If running the above command doesn't display the text correctly for you, perhaps your terminal isn't capable of displaying Unicode characters.
For information about reading Unicode data from a file, see this answer:
Character reading from file in Python
回答2:
Print a unicode character in Python:
Print a unicode character directly from python interpreter:
el@apollo:~$ python
Python 2.7.3
>>> print u'\u2713'
✓
Unicode character u'\u2713'
is a checkmark. The interpreter prints the checkmark on the screen.
Print a unicode character from a python script:
Put this in test.py:
#!/usr/bin/python
print("here is your checkmark: " + u'\u2713');
Run it like this:
el@apollo:~$ python test.py
here is your checkmark: ✓
If it doesn't show a checkmark for you, then the problem could be elsewhere, like the terminal settings or something you are doing with stream redirection.
Store unicode characters in a file:
Save this to file: foo.py:
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
import codecs
import sys
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)
print(u'e with obfuscation: é')
Run it and pipe output to file:
python foo.py > tmp.txt
Open tmp.txt and look inside, you see this:
el@apollo:~$ cat tmp.txt
e with obfuscation: é
Thus you have saved unicode e with a obfuscation mark on it to a file.
回答3:
If you're trying to print()
Unicode, and getting ascii codec errors, check out this page, the TLDR of which is do export PYTHONIOENCODING=UTF-8
before firing up python (this variable controls what sequence of bytes the console tries to encode your string data as). Internally, Python3 uses UTF-8 by default (see the Unicode HOWTO) so that's not the problem; you can just put Unicode in strings, as seen in the other answers and comments. It's when you try and get this data out to your console that the problem happens. Python thinks your console can only handle ascii. Some of the other answers say, "Write it to a file, first" but note they specify the encoding (UTF-8) for doing so (so, Python doesn't change anything in writing), and then use a method for reading the file that just spits out the bytes without any regard for encoding, which is why that works.
回答4:
In Python 2, you declare unicode strings with a u
, as in u"猫"
and use decode()
and encode()
to translate to and from unicode, respectively.
It's quite a bit easier in Python 3. A very good overview can be found here. That presentation clarified a lot of things for me.
回答5:
I use Portable winpython in Windows, it includes IPython QT console, I could achieve the following.
>>>print ("結婚")
結婚
>>>print ("おはよう")
おはよう
>>>str = "結婚"
>>>print (str)
結婚
your console interpreter should support unicode in order to show unicode characters.
回答6:
This fixes UTF-8 printing in python:
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)
回答7:
Considering that this is the first stack overflow result when google searching this topic, it bears mentioning that prefixing u
to unicode strings is optional in Python 3. (Python 2 example was copied from the top answer)
Python 3 (both work):
print('\u0420\u043e\u0441\u0441\u0438\u044f')
print(u'\u0420\u043e\u0441\u0441\u0438\u044f')
Python 2:
print u'\u0420\u043e\u0441\u0441\u0438\u044f'
回答8:
Just one more thing that hasn't been added yet
In Python 2, if you want to print a variable that has unicode and use .format()
, then do this (make the base string that is being formatted a unicode string with u''
:
>>> text = "Université de Montréal"
>>> print(u"This is unicode: {}".format(text))
>>> This is unicode: Université de Montréal
来源:https://stackoverflow.com/questions/10569438/how-to-print-unicode-character-in-python