关于linux输出重定向(1>/dev/null)的一个疑惑

一个Python脚本 展示结巴分词结果 如下所示

test.py
# -*- coding: utf-8 -*-
import jieba
print("/".join(jieba.cut("hello world")))
print("/".join(jieba.cut("你好世界")))

运行该脚本 一切正常

python test.py            
Building prefix dict from the default dictionary ...
Loading model from cache /var/folders/1k/p6dwx_0x28b6y752c1269hhw0000gn/T/jieba.cache
Loading model cost 0.398 seconds.
Prefix dict has been built succesfully.
hello/ /world
你好/世界

不想展示结巴相关的一些多余信息 于是通过Linux重定向的方式屏蔽这些信息 如下所示

python  test.py 2>/dev/null
hello/ /world
你好/世界

于是可以==》 如果将正常输出屏蔽掉的话(1>/dev/null) 应该只会显示 python test.py 1>/dev/null = python test.py - python test.py 2>/dev/null

Building prefix dict from the default dictionary ...
Loading model from cache /var/folders/1k/p6dwx_0x28b6y752c1269hhw0000gn/T/jieba.cache
Loading model cost 0.398 seconds.
Prefix dict has been built succesfully.

但是发现实际输出的更多

python  test.py 1>/dev/null
Building prefix dict from the default dictionary ...
Loading model from cache /var/folders/1k/p6dwx_0x28b6y752c1269hhw0000gn/T/jieba.cache
Loading model cost 0.379 seconds.
Prefix dict has been built succesfully.
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    print("/".join(jieba.cut("你好世界")))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

这多出来的输出是怎么来的啊?怎么一开始执行 python test.py的时候就没有这个输出呢?

阅读 6.4k
3 个回答

你是 Python 2 么?Python 2 默认重定向到文件时是 ascii 编码的输出,在输出中文时会出错的。

解决方案是设置 PYTHONIOENCODING 为 utf-8,或者使用 Python 3。

兄弟报错了,报错了,报错了!

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

Python里面中文务必通过编码工具稍作处理:http://wklken.me/posts/2013/0...

楼上+1,都什么年代了,还在坚守python2?官方都弃疗了。python2在终端重定向以及管道处理上就是会各种问题,还有非ascii字符的处理简直蛋疼。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题