【发布时间】:2018-06-20 13:48:55
【问题描述】:
我使用 2.7 编写的 Python 脚本(seafile-cli,来自 Seafile,一个文件同步解决方案)。
我知道unicode is problematic in Python 2,但幸运的是在启动脚本时正确处理了带有变音符号的文件名:
$ # seaf-cli status
# Name Status Progress
photos downloading 0/0, 0.0KB/s
Ma bibliothèque downloading 566/1770, 1745.7KB/s
videos downloading 28/1203, 5088.0KB/s
dev-perso downloading 0/0, 0.0KB/s
dev-pro downloading 0/0, 0.0KB/s
令我惊讶的是,当管道输出此输出时,Python 脚本崩溃并显示 UnicodeEncodeError:
$ seaf-cli status | cat -
# Name Status Progress
photos downloading 0/0, 0.0KB/s
Traceback (most recent call last):
File "/usr/bin/seaf-cli", line 845, in <module>
main()
File "/usr/bin/seaf-cli", line 841, in main
args.func(args)
File "/usr/bin/seaf-cli", line 649, in seaf_status
tx_task.rate/1024.0)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 11: ordinal not in range(128)
虽然我知道它可能首先与 Ma bibliothèque 存在问题(它没有),但为什么管道它会触发 Traceback?
不应该是shell的问题吗? - 输出在那个时候已经“离开”了脚本。
编辑:answer 在another question 中。标记为重复。
【问题讨论】:
标签: python shell unicode pipe python-unicode