在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保存到文件中。