
- Python 2和Python最大的区别就是字符编码上的区别,通常在Python 2的文件头都需要声明为UTF-8编码:
1 |
-*- coding: utf-8 -*- |
因此对于Python 2的中文符需要编码(先转为Unicode再进行encode)
1 2 3 4 |
# Windows my_words.decode("gb2312").encode("utf-8") # Linux my_words.decode("utf-8").encode("utf-8") |
而Python中则直接采用了Unicode编码,无需进行转码
SyntaxError: Missing parentheses in call to ‘print’
在Python 3中print后需添加括号进行打印输出
NameError: name ‘xrange’ is not defined
Python 2下的xrange在Python 3中使用range
AttributeError: ‘dict’ object has no attribute ‘itermvalues’
Python 3对于原Python 2下的iteritems, iterkeys, itervalues一律去除iter