python输出unicode字符_如何在Python中打印Unicode字符?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值