Skip to main content
 首页 » 编程设计

python之为什么重定向不适用于 python 脚本

2025年05月04日7xxx_UU

<分区>

Possible Duplicate:
Setting the correct encoding when piping stdout in python

以下按预期在 python shell (2.7.3) 中运行

for i in range(999): 
    print i, unichr(i) 

将其保存在文件(asd.py)中,并在shell中运行

$ ./asd.py 

也可以,但是

$ ./asd.py > asd.txt 

给出:

Traceback (most recent call last): 
  File "./asd.py", line 3, in <module> 
    print i, unichr(i) 
UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128) 

这是为什么呢?如何解决?