2016-01-19 24 views
0

guys!我在Debian 7终端上试图运行一个通过电报-cli发送消息的python脚本。下面这行给我一个错误:Debian终端中的UnicodeEncodeError

check_call(["/usr/local/tg/bin/telegram-cli", "-W", "-k", "/usr/local/tg/tg-server.pub", "-e", msg]) 

在此行结束后,“味精”是一个变量...

的错误是这样的:

Traceback (most recent call last): 
    File "LogServicos.py", line 60, in <module> 
    msg_telegram() 
    File "LogServicos.py", line 17, in msg_telegram 
    check_call(["/usr/local/tg/bin/telegram-cli", "-W", "-k", "/usr/local/tg/tg-server.pub", "-e", msg]) 
    File "/usr/local/lib/python3.5/subprocess.py", line 579, in check_call 
    retcode = call(*popenargs, **kwargs) 
    File "/usr/local/lib/python3.5/subprocess.py", line 560, in call 
    with Popen(*popenargs, **kwargs) as p: 
    File "/usr/local/lib/python3.5/subprocess.py", line 950, in __init__ 
    restore_signals, start_new_session) 
    File "/usr/local/lib/python3.5/subprocess.py", line 1483, in _execute_child 
    restore_signals, start_new_session, preexec_fn) 
UnicodeEncodeError: 'ascii' codec can't encode character '\xe7' in position 25: ordinal not in range(128) 

的脚本完全功能在我的debian 15.10使用python 3.5.1,就像在Debian中一样。

任何帮助?谢谢!

回答

0

传递给telegram-cli的命令行参数包含非ascii字符,在这种情况下,msg包含字符ç\xe7)。在* nix上,文件名和命令行参数最后只是字节,所以需要将unicode字符串转换为像这样使用的字节。

python uses sys.getfilesystemencoding()这样的转换,通常取决于LANGLC_*环境变量。

在您的情况下,在该程序失败的计算机上,默认语言环境仅支持ascii(可能是C语言环境),您可以使用locale命令查看哪一个被正确使用。

要修正这个错误,你可以:

  • 编码msg它传递到命令行的时候,但只会工作,如果正确telepathy-client不还依赖于当前语言环境来解释它,否则最终可能会出现垃圾字符
  • 请确保在运行脚本的环境中设置了正确的语言环境。您可以使用locale -a来检查系统支持哪些语言环境,如果没有合适的语言环境显示在列表中,则可能需要安装合适的新语言环境(如描述的herehere