【发布时间】:2015-03-03 16:48:51
【问题描述】:
我有以下小型 python3 测试程序(称为:testing)在 Raspberry Pi 上以 root 运行,打印三个 ascii 字符 (abc) 和三个 Latin-1 源编码的瑞典字符 (åäö):
#!/usr/bin/env python3
# -*- coding: Latin-1 -*-
print('abc')
print('åäö')
在具有远程字符集 = UTF-8 的 Putty SSH 终端中运行它没有问题。 终端上的输出显示为:
abc
åäö
在 root 下使用相同的 python3 程序从相同的 Putty 会话运行分离的 screen 命令也没有问题:
screen -dm -U -L -S testing /root/testing
在screen命令日志中screenlog.0可以看到打印的是:
abc in hex: 61 62 63
åäö in hex: C3 A5 C3 A4 C3 B6
然后我尝试在启动期间从 rc.local 中的脚本(称为:testingscreen)运行相同的分离 screen 命令:
在 rc.local 中添加了 '/root/testingscreen' 并且该脚本包含与上述相同的 screen 命令:
#!/bin/sh
screen -dm -U -L -S testing /root/testing
在这种情况下会发生错误,并在 screenlog.0 中写入以下内容:
abc
Traceback (most recent call last):
File "/root/testing", line 4, in <module>
print('\xe5\xe4\xf6')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
请注意,我在 screen 命令中使用了 -U 参数,其含义如下:“以 UTF-8 模式运行屏幕。此选项告诉屏幕您的终端发送和理解 UTF-8 编码字符。它还将新窗口的默认编码设置为 'utf8'。"
为什么行为不同,我做错了什么?
【问题讨论】:
标签: python-3.x encoding utf-8 raspberry-pi gnu-screen