![](/img/trans.png)
[英]Python 2.7.6 - UnicodeEncodeError in Sublime 2 but NO error in Terminal
[英]UnicodeEncodeError in a Debian terminal
伙计们! 我在Debian 7终端中尝试运行通过电报CLI发送消息的python脚本。 以下行给我一个错误:
check_call(["/usr/local/tg/bin/telegram-cli", "-W", "-k", "/usr/local/tg/tg-server.pub", "-e", msg])
在此行的结尾,“ 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)
该脚本在使用python 3.5.1的debian 15.10上完全可用,与Debian中的脚本相同。
有什么帮助吗? 谢谢!
传递给telegram-cli
的命令行参数包含非ASCII字符,在这种情况下, msg
包含字符ç
( \\xe7
)。 在* nix上,文件名和命令行参数最后只是字节,因此unicode字符串需要转换为字节才能使用。
python 使用sys.getfilesystemencoding()
进行此类转换,通常取决于LANG
和LC_*
环境变量。
在此程序失败的计算机上,默认语言环境仅支持ascii(可能是C
语言环境),您可以使用locale
命令查看确切使用的locale
。
要解决此错误,您可以:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.