在Python中打印unicode字符:
直接从python解释器打印一个unicode字符:
el@apollo:~$ python
Python 2.7.3
>>> print u'\u2713'
✓
Unicode字符u'\u2713'是一个复选标记。 解释器在屏幕上打印复选标记。
从python脚本中打印unicode字符:
把它放在test.py中:
#!/usr/bin/python
print("here is your checkmark: " + u'\u2713');
像这样运行:
el@apollo:~$ python test.py
here is your checkmark: ✓
如果它没有为您显示复选标记,则问题可能出在其他地方,例如终端设置或您正在使用流重定向执行的操作。
将unicode字符存储在文件中:
将其保存到文件: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: é')
运行它并将输出管道输出到文件:
python foo.py > tmp.txt
打开tmp.txt并查看内部,你会看到:
el@apollo:~$ cat tmp.txt
e with obfuscation: é
因此,您已将带有模糊标记的unicode e保存到文件中。