ssh执行远程python脚本utf-8的问题,报Unicode

2020-06-08  本文已影响0人  风吹路过的云

需要ssh远程执行一个python脚本,在一个节点上是正常的,环境是centos7,bash。
在另一台节点上居然报错了,环境是centos7,zsh,报错信息如下:

ssh root@xx.xx.xx.xx "python3 /data/scripts/demo.py"
UnicodeEncodeError: 'ascii' codec can't encode characters in position 144-150: ordinal not in range(128)

好明显是编码问题,检查了python文件的编码,文件头有coding utf-8,编码也是utf-8

#!/usr/bin/python3
# coding=utf-8

接着检查两个环境的env,locale,都是一样

[root@xx ~]# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

那问题出在哪里,随后google之,有文章说要加PYTHONIOENCODING=utf-8,即执行命令如下:

ssh root@xx.xx.xx.xx "PYTHONIOENCODING=utf-8 python3 /data/scripts/demo.py"

问题解决!:)

相关链接
https://stackoverflow.com/questions/17374526/unicodeencodeerror-when-using-python-from-ssh

上一篇下一篇

猜你喜欢

热点阅读