wulala1234's recent timeline updates
wulala1234

wulala1234

V2EX member #308828, joined on 2018-04-15 14:57:10 +08:00
wulala1234's recent replies
@Sylv 非常感谢您的回复。
在终端里
$locale
LANG="zh_CN.UTF-8"
LC_COLLATE="zh_CN.UTF-8"
LC_CTYPE="zh_CN.UTF-8"
LC_MESSAGES="zh_CN.UTF-8"
LC_MONETARY="zh_CN.UTF-8"
LC_NUMERIC="zh_CN.UTF-8"
LC_TIME="zh_CN.UTF-8"
LC_ALL=

LC_ALL 无默认值

$ python 3.5
>>> import locale
>>> locale.getpreferredencoding()
'UTF-8'

而在 sublime Text3 中
# python3.5
>>>import locale
>>>print (locale.getpreferredencoding())
US-ASCII

>>>print (locale.getdefaultlocale())
(None, None)

因为编码不同产生了错误。

您上面的回复是一种解决方式

还可以在代码中临时设置语言环境

>>>locale.setlocale(locale.LC_ALL,'zh_CN.UTF-8')

解决问题。

不过您的解决方式是一劳永逸的。

参考:
https://docs.python.org/2/library/locale.html#locale.getdefaultlocale

接下来去查查 sys.getdefaultencoding()与 locale.getdefaultlocale()的区别了~~~
@hicdn 终端下( Mac OS X ) python3.5.1 和 python3.6.4 同运行正常。但在 sublime Text 3 中结果就是解码错误。
@zhjits 查看 help(os.popen) 信息如下:
```
Help on function popen in module os:

popen(cmd, mode='r', buffering=-1)
# Supply os.popen()
```
查询到的信息:参数 mode 只支持‘ r'(默认),'w',两种模式。。。

代入‘ rb ’,报错如下:
```
raise ValueError("invalid mode %r" % mode)
ValueError: invalid mode 'rb'
```
About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2433 Online   Highest 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 20ms · UTC 11:45 · PVG 19:45 · LAX 04:45 · JFK 07:45
♥ Do have faith in what you're doing.