python2与python3 decode用法完全不同

2018-06-23  本文已影响16人  sexy_cyber

这是Python2:

Python 2.7.10 (default, Oct  6 2017, 22:29:07) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> name = 'long'
>>> name = '龙'
>>> name.decode('utf-8')
u'\u9f99'
>>> 


这是python3:

Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> name = '龙'
>>> name.decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'decode'
>>> 


用惯了python3的我竟然不知所措;

上一篇下一篇

猜你喜欢

热点阅读