open 中 w 与 wb 区别

2020-05-08  本文已影响0人  lindyang

PY3 对应原则

PY2

并没有像 PY3 一样遵守对应原则

open('/tmp/_', 'wb').write(u'汉字')

也是可以的。可能的报错

UnicodeEncodeError: 'ascii' codec can't encode characters...

只是指明默认的 ascii 规则不能 encode 汉字,此时需要指定默认编码

import sys
try:
    reload(sys)
    sys.setdefaultencoding('utf-8')
except NameError:
    pass

PY2、PY3 字符串的区别

变量 'a' u'a' b'a'
PY2 <type 'str'> <type 'unicode'> <type 'str'>
PY3 <class 'str'> <class 'str'> <class 'bytes'>

pipe = Popen(["ls"], stdout=PIPE).stdout
for line in iter(pipe.readline, b'')

上一篇 下一篇

猜你喜欢

热点阅读