Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Debian终端中的UnicodeError_Python_Unicode_Terminal_Debian - Fatal编程技术网

Python Debian终端中的UnicodeError

Python Debian终端中的UnicodeError,python,unicode,terminal,debian,Python,Unicode,Terminal,Debian,伙计们!我在Debian 7终端中尝试运行python脚本,该脚本通过telegram cli发送消息。下面这行给了我一个错误: 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",

伙计们!我在Debian 7终端中尝试运行python脚本,该脚本通过telegram cli发送消息。下面这行给了我一个错误:

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)
回溯(最近一次呼叫最后一次):
文件“LogServicos.py”,第60行,在
电报
msg_电报中第17行的文件“LogServicos.py”
检查调用([“/usr/local/tg/bin/telegram cli”、“-W”、“-k”、“/usr/local/tg/tg server.pub”、“-e”、msg])
文件“/usr/local/lib/python3.5/subprocess.py”,第579行,在check_调用中
retcode=call(*popenargs,**kwargs)
文件“/usr/local/lib/python3.5/subprocess.py”,第560行,在调用中
将Popen(*popenargs,**kwargs)作为p:
文件“/usr/local/lib/python3.5/subprocess.py”,第950行,在__
恢复信号,启动新会话)
文件“/usr/local/lib/python3.5/subprocess.py”,第1483行,在执行子进程中
恢复信号,启动新会话,preexec\u fn)
UnicodeEncodeError:“ascii”编解码器无法对位置25中的字符“\xe7”进行编码:序号不在范围内(128)
该脚本在使用python 3.5.1的debian 15.10上完全可用,与debian中的脚本相同


有什么帮助吗?谢谢

传递到
电报cli
的命令行参数包含非ascii字符,在本例中
msg
包含字符
ç
\xe7
)。在*nix上,文件名和命令行参数最后都是字节,因此需要将unicode字符串转换为字节才能使用

python用于此类转换,这通常取决于
LANG
LC.*
环境变量

在本程序失败的计算机上,默认区域设置仅支持ascii(可能是
C
locale),您可以使用
locale
命令查看到底使用了哪一个

要修复此错误,您可以:

  • msg
    传递到命令行时进行编码,但只有在
    teletathy client
    不依赖当前区域设置来解释它时,编码才能正常工作,否则可能会导致垃圾字符
  • 确保在运行脚本的环境中设置了正确的区域设置。您可以使用
    locale-a
    检查系统支持哪些区域设置,如果列表中没有显示合适的区域设置,您可能需要安装合适的新区域设置(如所述或所述)