python中python2和python3之间怎么转换? python2和python3的区别,转换及共存

\u5982\u4f55\u628aPython2\u7684\u4ee3\u7801\u8f6c\u6362\u4e3aPython3\u7684\u4ee3\u7801

\u53ef\u4ee5\u5229\u7528Python\u5185\u7f6e\u5de5\u51772to3.py\uff0c\u5e2e\u4f60\u81ea\u52a8\u5c06Python2\u7684\u4ee3\u7801\u8f6c\u6362\u4e3aPython3\u7684\u4ee3\u7801\u7684\u3002\u8fd9\u4e2a\u811a\u672c\u7684\u4f4d\u7f6e\u4f4d\u5728Python\u5b89\u88c5\u7684\u6839\u76ee\u5f55\Python27\Tools\Scripts\2to3.py\u3002
\u6bd4\u5982\u6211\u624b\u4e0a\u6709\u4e2aPython 2.x\u7684python\u811a\u672c\uff1aD:\tmp\transform.py
\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff0c\u5b9a\u4f4d\u81f3\u8be5\u8981\u8f6c\u6362\u7684\u811a\u672c\u76ee\u5f55\u4e0b\uff0c\u7136\u540e\u8fd0\u884c
2to3.py -w transform.py

\u5982\u679c\u4e0a\u8ff0\u4e0d\u52a0-w\u53c2\u6570\uff0c\u5219\u9ed8\u8ba4\u53ea\u662f\u628a\u8f6c\u6362\u8fc7\u7a0b\u6240\u5bf9\u5e94\u7684diff\u5185\u5bb9\u6253\u5370\u8f93\u51fa\u5230\u5f53\u524d\u7a97\u53e3\u800c\u5df2\u3002\u52a0\u4e86-w\uff0c\u5c31\u662f\u628a\u6539\u52a8\u5185\u5bb9\uff0c\u5199\u56de\u5230\u539f\u5148\u7684\u6587\u4ef6\u4e86\u3002

python2\u548cpython3\u7684\u533a\u522b
1.\u6027\u80fd
Py3.0\u8fd0\u884c pystone benchmark\u7684\u901f\u5ea6\u6bd4Py2.5\u616230%\u3002Guido\u8ba4\u4e3aPy3.0\u6709\u6781\u5927\u7684\u4f18\u5316\u7a7a\u95f4\uff0c\u5728\u5b57\u7b26\u4e32\u548c\u6574\u5f62\u64cd\u4f5c\u4e0a\u53ef\u4ee5\u53d6\u5f97\u5f88\u597d\u7684\u4f18\u5316\u7ed3\u679c\u3002
Py3.1\u6027\u80fd\u6bd4Py2.5\u616215%\uff0c\u8fd8\u6709\u5f88\u5927\u7684\u63d0\u5347\u7a7a\u95f4\u3002
2.\u7f16\u7801
Py3.X\u6e90\u7801\u6587\u4ef6\u9ed8\u8ba4\u4f7f\u7528utf-8\u7f16\u7801\uff0c\u8fd9\u5c31\u4f7f\u5f97\u4ee5\u4e0b\u4ee3\u7801\u662f\u5408\u6cd5\u7684\uff1a >>> \u4e2d\u56fd = 'china' >>>print(\u4e2d\u56fd) china
3. \u8bed\u6cd5
1\uff09\u53bb\u9664\u4e86\uff0c\u5168\u90e8\u6539\u7528!=
\u5728Python 2\u91cc\uff0c\u4e3a\u4e86\u5f97\u5230\u4e00\u4e2a\u4efb\u610f\u5bf9\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u6709\u4e00\u79cd\u628a\u5bf9\u8c61\u5305\u88c5\u5728\u53cd\u5f15\u53f7\u91cc(\u6bd4\u5982`x`)\u7684\u7279\u6b8a\u8bed\u6cd5\u3002\u5728Python 3\u91cc\uff0c\u8fd9\u79cd\u80fd\u529b\u4ecd\u7136\u5b58\u5728\uff0c\u4f46\u662f\u4f60\u4e0d\u80fd\u518d\u4f7f\u7528\u53cd\u5f15\u53f7\u83b7\u5f97\u8fd9\u79cd\u5b57\u7b26\u4e32\u8868\u793a\u4e86\u3002\u4f60\u9700\u8981\u4f7f\u7528\u5168\u5c40\u51fd\u6570repr()\u3002

Notes
Python 2
Python 3


\u2460
`x` repr(x)


\u2461
`'PapayaWhip' + `2`` repr('PapayaWhip'+ repr(2))

Note:x\u53ef\u4ee5\u662f\u4efb\u4f55\u4e1c\u897f \u2014 \u4e00\u4e2a\u7c7b\uff0c\u51fd\u6570\uff0c\u6a21\u5757\uff0c\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u7b49\u7b49\u3002repr()\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u7c7b\u578b\u7684\u53c2\u6570\u3002
2\uff09\u53bb\u9664``\uff0c\u5168\u90e8\u6539\u7528repr()
3\uff09\u5173\u952e\u8bcd\u52a0\u5165as \u548cwith\uff0c\u8fd8\u6709True,False,None
4\uff09\u6574\u578b\u9664\u6cd5\u8fd4\u56de\u6d6e\u70b9\u6570\uff0c\u8981\u5f97\u5230\u6574\u578b\u7ed3\u679c\uff0c\u8bf7\u4f7f\u7528//
\u7531\u4e8e\u4eba\u4eec\u5e38\u5e38\u4f1a\u5ffd\u89c6Python 3\u5728\u6574\u6570\u9664\u6cd5\u4e0a\u7684\u6539\u52a8\uff08\u5199\u9519\u4e86\u4e5f\u4e0d\u4f1a\u89e6\u53d1Syntax Error\uff09\uff0c\u6240\u4ee5\u5728\u79fb\u690d\u4ee3\u7801\u6216\u5728Python 2\u4e2d\u6267\u884cPython 3\u7684\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u8fd9\u4e2a\u6539\u52a8\u3002
\u6240\u4ee5\uff0c\u6211\u8fd8\u662f\u4f1a\u5728Python 3\u7684\u811a\u672c\u4e2d\u5c1d\u8bd5\u7528float(3)/2\u6216 3/2.0\u4ee3\u66ff3/2\uff0c\u4ee5\u6b64\u6765\u907f\u514d\u4ee3\u7801\u5728Python 2\u73af\u5883\u4e0b\u53ef\u80fd\u5bfc\u81f4\u7684\u9519\u8bef\uff08\u6216\u4e0e\u4e4b\u76f8\u53cd\uff0c\u5728Python 2\u811a\u672c\u4e2d\u7528from __future__ import division\u6765\u4f7f\u7528Python 3\u7684\u9664\u6cd5\uff09\u3002
Python 2
print '3 / 2 =', 3 / 2 print '3 // 2 =', 3 // 2 print '3 / 2.0 =', 3 / 2.0 print '3 // 2.0 =', 3 // 2.0 3 / 2 = 1 3 // 2 = 1 3 / 2.0 = 1.5 3 // 2.0 = 1.0
\u9ed8\u8ba4\uff0c\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u6574\u6570\uff0cPython 2 \u81ea\u52a8\u6267\u884c\u6574\u578b\u8ba1\u7b97\u3002
Python 3
print('3 / 2 =', 3 / 2) print('3 // 2 =', 3 // 2) print('3 / 2.0 =', 3 / 2.0) print('3 // 2.0 =', 3 // 2.0) 3 / 2 = 1.5 3 // 2 = 1 3 / 2.0 = 1.5 3 // 2.0 = 1.0
Note: \u9700\u8981\u6ce8\u610f\u7684\u662f\u201c\u53cc\u5212\u7ebf\u201d\uff08//\uff09\u64cd\u4f5c\u7b26\u5c06\u4e00\u76f4\u6267\u884c\u6574\u9664\uff0c\u800c\u4e0d\u7ba1\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 5.0//2.0 \u503c\u4e3a 2.0\u3002Python 3 \u4e2d\uff0c/ \u64cd\u4f5c\u7b26\u662f\u505a\u6d6e\u70b9\u9664\u6cd5\uff0c\u800c // \u662f\u505a\u6574\u9664\uff08\u5373\u5546\u6ca1\u6709\u4f59\u6570\uff0c\u6bd4\u5982 10 // 3 \u5176\u7ed3\u679c\u5c31\u4e3a 3\uff0c\u4f59\u6570\u4f1a\u88ab\u622a\u9664\u6389\uff0c\u800c (-7) // 3 \u7684\u7ed3\u679c\u5374\u662f -3\u3002\u8fd9\u4e2a\u7b97\u6cd5\u4e0e\u5176\u5b83\u5f88\u591a\u7f16\u7a0b\u8bed\u8a00\u4e0d\u4e00\u6837\uff0c\u9700\u8981\u6ce8\u610f\uff0c\u5b83\u4eec\u7684\u6574\u9664\u8fd0\u7b97\u4f1a\u54110\u7684\u65b9\u5411\u53d6\u503c\u3002\u800c\u5728 Python 2 \u4e2d\uff0c/ \u5c31\u662f\u6574\u9664\uff0c\u5373\u548c Python 3 \u4e2d\u7684 // \u64cd\u4f5c\u7b26\u4e00\u6837\u3002
5\uff09\u52a0\u5165nonlocal\u8bed\u53e5\u3002\u4f7f\u7528noclocal x\u53ef\u4ee5\u76f4\u63a5\u6307\u6d3e\u5916\u56f4\uff08\u975e\u5168\u5c40\uff09\u53d8\u91cf
6\uff09print
\u53bb\u9664print\u8bed\u53e5\uff0c\u52a0\u5165print()\u51fd\u6570\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002\u540c\u6837\u7684\u8fd8\u6709 exec\u8bed\u53e5\uff0c\u5df2\u7ecf\u6539\u4e3aexec()\u51fd\u6570
\u5728Python 2\u91cc\uff0cprint\u662f\u4e00\u4e2a\u8bed\u53e5\u3002\u65e0\u8bba\u4f60\u60f3\u8f93\u51fa\u4ec0\u4e48\uff0c\u53ea\u8981\u5c06\u5b83\u4eec\u653e\u5728print\u5173\u952e\u5b57\u540e\u8fb9\u5c31\u53ef\u4ee5\u3002
Python 3\u91cc\uff0cprint()\u662f\u4e00\u4e2a\u51fd\u6570\u3002\u5c31\u50cf\u5176\u4ed6\u7684\u51fd\u6570\u4e00\u6837\uff0cprint()\u9700\u8981\u4f60\u5c06\u60f3\u8981\u8f93\u51fa\u7684\u4e1c\u897f\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9\u5b83\u3002
\u4f8b\u5982\uff1a 2.X: print "The answer is", 2*2 3.X: print("The answer is", 2*2) 2.X: print x, # \u4f7f\u7528\u9017\u53f7\u7ed3\u5c3e\u7981\u6b62\u6362\u884c 3.X: print(x, end=" ") # \u4f7f\u7528\u7a7a\u683c\u4ee3\u66ff\u6362\u884c
\u5728Python 2\u91cc\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e00\u4e2a\u9017\u53f7(,)\u4f5c\u4e3aprint\u8bed\u53e5\u7684\u7ed3\u5c3e\uff0c\u5b83\u5c06\u4f1a\u7528\u7a7a\u683c\u5206\u9694\u8f93\u51fa\u7684\u7ed3\u679c\uff0c\u7136\u540e\u5728\u8f93\u51fa\u4e00\u4e2a\u5c3e\u968f\u7684\u7a7a\u683c(trailing space)\uff0c\u800c\u4e0d\u8f93\u51fa\u56de\u8f66(carriage return)\u3002\u5728Python 3\u91cc\uff0c\u901a\u8fc7\u628aend=' '\u4f5c\u4e3a\u4e00\u4e2a\u5173\u952e\u5b57\u53c2\u6570\u4f20\u7ed9print()\u53ef\u4ee5\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002\u53c2\u6570end\u7684\u9ed8\u8ba4\u503c\u4e3a'\n'\uff0c\u6240\u4ee5\u901a\u8fc7\u91cd\u65b0\u6307\u5b9aend\u53c2\u6570\u7684\u503c\uff0c\u53ef\u4ee5\u53d6\u6d88\u5728\u672b\u5c3e\u8f93\u51fa\u56de\u8f66\u7b26\u30022.X: print # \u8f93\u51fa\u65b0\u884c 3.X: print() # \u8f93\u51fa\u65b0\u884c 2.X: print >>sys.stderr, "fatal error" 3.X: print("fatal error", file=sys.stderr)
\u5728Python 2\u91cc\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528>>pipe_name\u8bed\u6cd5\uff0c\u628a\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u7ba1\u9053\uff0c\u6bd4\u5982sys.stderr\u3002\u5728Python 3\u91cc\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5c06\u7ba1\u9053\u4f5c\u4e3a\u5173\u952e\u5b57\u53c2\u6570file\u7684\u503c\u4f20\u9012\u7ed9print()\u6765\u5b8c\u6210\u540c\u6837\u7684\u529f\u80fd\u3002\u53c2\u6570file\u7684\u9ed8\u8ba4\u503c\u4e3astd.stdout\uff0c\u6240\u4ee5\u91cd\u65b0\u6307\u5b9a\u5b83\u7684\u503c\u5c06\u4f1a\u4f7fprint()\u8f93\u51fa\u5230\u4e00\u4e2a\u53e6\u5916\u4e00\u4e2a\u7ba1\u9053\u30022.X: print (x, y) # \u8f93\u51farepr((x, y)) 3.X: print((x, y)) # \u4e0d\u540c\u4e8eprint(x, y)!
exec\u8bed\u53e5
exec()\u51fd\u6570\u4f7f\u7528\u4e00\u4e2a\u5305\u542b\u4efb\u610fPython\u4ee3\u7801\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u5c31\u50cf\u6267\u884c\u8bed\u53e5\u6216\u8005\u8868\u8fbe\u5f0f\u4e00\u6837\u6267\u884c\u5b83\u3002exec()\u8ddfeval()\u662f\u76f8\u4f3c\u7684\uff0c\u4f46\u662fexec()\u66f4\u52a0\u5f3a\u5927\u5e76\u66f4\u5177\u6709\u6280\u5de7\u6027\u3002eval()\u51fd\u6570\u53ea\u80fd\u6267\u884c\u5355\u72ec\u4e00\u6761\u8868\u8fbe\u5f0f\uff0c\u4f46\u662fexec()\u80fd\u591f\u6267\u884c\u591a\u6761\u8bed\u53e5\uff0c\u5bfc\u5165(import)\uff0c\u51fd\u6570\u58f0\u660e \u2014 \u5b9e\u9645\u4e0a\u6574\u4e2aPython\u7a0b\u5e8f\u7684\u5b57\u7b26\u4e32\u8868\u793a\u4e5f\u53ef\u4ee5\u3002

Notes
Python 2
Python 3


\u2460
exec codeString exec(codeString)


\u2461
exec codeString in a_global_namespace exec(codeString, a_global_namespace)


\u2462
exec codeString in a_global_namespace, a_local_namespace exec(codeString, a_global_namespace, a_local_namespace)

\u5728\u6700\u7b80\u5355\u7684\u5f62\u5f0f\u4e0b\uff0c\u56e0\u4e3aexec()\u73b0\u5728\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u8bed\u53e5\uff0c2to3\u4f1a\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u4ee3\u7801\u7528\u62ec\u53f7\u56f4\u8d77\u6765\u3002
Python 2\u91cc\u7684exec\u8bed\u53e5\u53ef\u4ee5\u6307\u5b9a\u540d\u5b57\u7a7a\u95f4\uff0c\u4ee3\u7801\u5c06\u5728\u8fd9\u4e2a\u7531\u5168\u5c40\u5bf9\u8c61\u7ec4\u6210\u7684\u79c1\u6709\u7a7a\u95f4\u91cc\u6267\u884c\u3002Python 3\u4e5f\u6709\u8fd9\u6837\u7684\u529f\u80fd\uff1b\u4f60\u53ea\u9700\u8981\u628a\u8fd9\u4e2a\u540d\u5b57\u7a7a\u95f4\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9exec()\u51fd\u6570\u3002
\u66f4\u52a0\u795e\u5947\u7684\u662f\uff0cPython 2\u91cc\u7684exec\u8bed\u53e5\u8fd8\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u672c\u5730\u540d\u5b57\u7a7a\u95f4(\u6bd4\u5982\u4e00\u4e2a\u51fd\u6570\u91cc\u58f0\u660e\u7684\u53d8\u91cf)\u3002\u5728Python 3\u91cc\uff0cexec()\u51fd\u6570\u4e5f\u6709\u8fd9\u6837\u7684\u529f\u80fd\u3002
execfile\u8bed\u53e5
\u5c31\u50cf\u4ee5\u524d\u7684exec\u8bed\u53e5\uff0cPython 2\u91cc\u7684execfile\u8bed\u53e5\u4e5f\u53ef\u4ee5\u50cf\u6267\u884cPython\u4ee3\u7801\u90a3\u6837\u4f7f\u7528\u5b57\u7b26\u4e32\u3002\u4e0d\u540c\u7684\u662fexec\u4f7f\u7528\u5b57\u7b26\u4e32\uff0c\u800cexecfile\u5219\u4f7f\u7528\u6587\u4ef6\u3002\u5728Python 3\u91cc\uff0cexecfile\u8bed\u53e5\u5df2\u7ecf\u88ab\u53bb\u6389\u4e86\u3002\u5982\u679c\u4f60\u771f\u7684\u60f3\u8981\u6267\u884c\u4e00\u4e2a\u6587\u4ef6\u91cc\u7684Python\u4ee3\u7801(\u4f46\u662f\u4f60\u4e0d\u60f3\u5bfc\u5165\u5b83)\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\uff0c\u8bfb\u53d6\u5b83\u7684\u5185\u5bb9\uff0c\u7136\u540e\u8c03\u7528compile()\u5168\u5c40\u51fd\u6570\u5f3a\u5236Python\u89e3\u91ca\u5668\u7f16\u8bd1\u4ee3\u7801\uff0c\u7136\u540e\u8c03\u7528\u65b0\u7684exec()\u51fd\u6570\u3002

Notes
Python 2
Python 3



execfile('a_filename') exec(compile(open('a_filename').read(),'a_filename','exec'))
7\uff09\u8f93\u5165\u51fd\u6570\u6539\u53d8\u4e86\uff0c\u5220\u9664\u4e86raw_input\uff0c\u7528input\u4ee3\u66ff\uff1a Python 2\u6709\u4e24\u4e2a\u5168\u5c40\u51fd\u6570\uff0c\u7528\u6765\u5728\u547d\u4ee4\u884c\u8bf7\u6c42\u7528\u6237\u8f93\u5165\u3002\u7b2c\u4e00\u4e2a\u53eb\u505ainput()\uff0c\u5b83\u7b49\u5f85\u7528\u6237\u8f93\u5165\u4e00\u4e2aPython\u8868\u8fbe\u5f0f(\u7136\u540e\u8fd4\u56de\u7ed3\u679c)\u3002\u7b2c\u4e8c\u4e2a\u53eb\u505araw_input()\uff0c\u7528\u6237\u8f93\u5165\u4ec0\u4e48\u5b83\u5c31\u8fd4\u56de\u4ec0\u4e48\u3002\u8fd9\u8ba9\u521d\u5b66\u8005\u975e\u5e38\u56f0\u60d1\uff0c\u5e76\u4e14\u8fd9\u88ab\u5e7f\u6cdb\u5730\u770b\u4f5c\u662fPython\u8bed\u8a00\u7684\u4e00\u4e2a\u201c\u8089\u8d58\u201d(wart)\u3002Python 3\u901a\u8fc7\u91cd\u547d\u540draw_input()\u4e3ainput()\uff0c\u4ece\u800c\u5207\u6389\u4e86\u8fd9\u4e2a\u8089\u8d58\uff0c\u6240\u4ee5\u73b0\u5728\u7684input()\u5c31\u50cf\u6bcf\u4e2a\u4eba\u6700\u521d\u671f\u5f85\u7684\u90a3\u6837\u5de5\u4f5c\u30022.X:guess = int(raw_input('Enter an integer : ')) # \u8bfb\u53d6\u952e\u76d8\u8f93\u5165\u7684\u65b9\u6cd5 3.X:guess = int(input('Enter an integer : '))
Note:\u5982\u679c\u4f60\u771f\u7684\u60f3\u8981\u8bf7\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2aPython\u8868\u8fbe\u5f0f\uff0c\u8ba1\u7b97\u7ed3\u679c\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528input()\u51fd\u6570\u7136\u540e\u628a\u8fd4\u56de\u503c\u4f20\u9012\u7ed9eval()\u3002
I/O\u65b9\u6cd5xreadlines()
\u5728Python 2\u91cc\uff0c\u6587\u4ef6\u5bf9\u8c61\u6709\u4e00\u4e2axreadlines()\u65b9\u6cd5\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u4e00\u6b21\u8bfb\u53d6\u6587\u4ef6\u7684\u4e00\u884c\u3002\u8fd9\u5728for\u5faa\u73af\u4e2d\u5c24\u5176\u6709\u7528\u3002\u4e8b\u5b9e\u4e0a\uff0c\u540e\u6765\u7684Python 2\u7248\u672c\u7ed9\u6587\u4ef6\u5bf9\u8c61\u672c\u8eab\u6dfb\u52a0\u4e86\u8fd9\u6837\u7684\u529f\u80fd\u3002
\u5728Python 3\u91cc\uff0cxreadlines()\u65b9\u6cd5\u4e0d\u518d\u53ef\u7528\u4e86\u30022to3\u53ef\u4ee5\u89e3\u51b3\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4f46\u662f\u4e00\u4e9b\u8fb9\u7f18\u6848\u4f8b\u5219\u9700\u8981\u4eba\u5de5\u4ecb\u5165\u3002

Notes
Python 2
Python 3


\u2460
for line in a_file.xreadlines(): for line in a_file:

\u2461
for line in a_file.xreadlines(5): no change (broken)
\u5982\u679c\u4f60\u4ee5\u524d\u8c03\u7528\u6ca1\u6709\u53c2\u6570\u7684xreadlines()\uff0c2to3\u4f1a\u628a\u5b83\u8f6c\u6362\u6210\u6587\u4ef6\u5bf9\u8c61\u672c\u8eab\u3002\u5728Python 3\u91cc\uff0c\u8fd9\u79cd\u8f6c\u6362\u540e\u7684\u4ee3\u7801\u53ef\u4ee5\u5b8c\u6210\u524d\u540c\u6837\u7684\u5de5\u4f5c\uff1a\u4e00\u6b21\u8bfb\u53d6\u6587\u4ef6\u7684\u4e00\u884c\uff0c\u7136\u540e\u6267\u884cfor\u5faa\u73af\u7684\u5faa\u73af\u4f53\u3002
\u5982\u679c\u4f60\u4ee5\u524d\u4f7f\u7528\u4e00\u4e2a\u53c2\u6570(\u6bcf\u6b21\u8bfb\u53d6\u7684\u884c\u6570)\u8c03\u7528xreadlines()\uff0c2to3\u4e0d\u80fd\u4e3a\u4f60\u5b8c\u6210\u4ecePython 2\u5230Python 3\u7684\u8f6c\u6362\uff0c\u4f60\u7684\u4ee3\u7801\u4f1a\u4ee5\u8fd9\u6837\u7684\u65b9\u5f0f\u5931\u8d25\uff1aAttributeError: '_io.TextIOWrapper' object has no attribute 'xreadlines'\u3002\u4f60\u53ef\u4ee5\u624b\u5de5\u7684\u628axreadlines()\u6539\u6210readlines()\u4ee5\u4f7f\u4ee3\u7801\u80fd\u5728Python 3\u4e0b\u5de5\u4f5c\u3002(readline()\u65b9\u6cd5\u5728Python 3\u91cc\u8fd4\u56de\u8fed\u4ee3\u5668\uff0c\u6240\u4ee5\u5b83\u8ddfPython 2\u91cc\u7684xreadlines()\u6548\u7387\u662f\u4e0d\u76f8\u4e0a\u4e0b\u7684\u3002)
8\uff09\u6539\u53d8\u4e86\u987a\u5e8f\u64cd\u4f5c\u7b26\u7684\u884c\u4e3a\uff0c\u4f8b\u5982x<y\uff0c\u5f53x\u548cy\u7c7b\u578b\u4e0d\u5339\u914d\u65f6\u629b\u51faTypeError\u800c\u4e0d\u662f\u8fd4\u56de\u968f\u5373\u7684 bool\u503c
9\uff09\u53bb\u9664\u5143\u7ec4\u53c2\u6570\u89e3\u5305\u3002\u4e0d\u80fddef(a, (b, c)):pass\u8fd9\u6837\u5b9a\u4e49\u51fd\u6570\u4e86
10\uff09\u65b0\u5f0f\u76848\u8fdb\u5236\u5b57\u53d8\u91cf\uff0c\u76f8\u5e94\u5730\u4fee\u6539\u4e86oct()\u51fd\u6570\u30022.X\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a >>> 0666 438 >>> oct(438) '0666'
3.X\u8fd9\u6837\uff1a >>> 0666 SyntaxError: invalid token (, line 1) >>> 0o666 438 >>> oct(438) '0o666'
11\uff09\u589e\u52a0\u4e86 2\u8fdb\u5236\u5b57\u9762\u91cf\u548cbin()\u51fd\u6570 >>> bin(438) '0b110110110' >>> _438 = '0b110110110' >>> _438 '0b110110110'
12\uff09\u6269\u5c55\u7684\u53ef\u8fed\u4ee3\u89e3\u5305\u3002\u5728Py3.X \u91cc\uff0ca, b, *rest = seq\u548c *rest, a = seq\u90fd\u662f\u5408\u6cd5\u7684\uff0c\u53ea\u8981\u6c42\u4e24\u70b9\uff1arest\u662flist\u5bf9\u8c61\u548cseq\u662f\u53ef\u8fed\u4ee3\u7684\u3002
13\uff09\u65b0\u7684super()\uff0c\u53ef\u4ee5\u4e0d\u518d\u7ed9super()\u4f20\u53c2\u6570\uff0c>>> class C(object): def __init__(self, a): print('C', a) >>> class D(C): def __init(self, a): super().__init__(a) # \u65e0\u53c2\u6570\u8c03\u7528super() >>> D(8) C 8
14\uff09\u652f\u6301class decorator\u3002\u7528\u6cd5\u4e0e\u51fd\u6570decorator\u4e00\u6837\uff1a>>> def foo(cls_a): def print_func(self): print('Hello, world!') cls_a.print = print_func return cls_a >>> @foo class C(object): pass >>> C().print() Hello, world!
class decorator\u53ef\u4ee5\u7528\u6765\u73a9\u73a9\u72f8\u732b\u6362\u592a\u5b50\u7684\u5927\u628a\u620f\u3002\u66f4\u591a\u8bf7\u53c2\u9605PEP 3129
4. \u5b57\u7b26\u4e32\u548c\u5b57\u8282\u4e32
Python 2\u6709\u4e24\u79cd\u5b57\u7b26\u4e32\u7c7b\u578b\uff1aUnicode\u5b57\u7b26\u4e32\u548c\u975eUnicode\u5b57\u7b26\u4e32\u3002Python 2\u6709\u57fa\u4e8eASCII\u7684str()\u7c7b\u578b\uff0c\u5176\u53ef\u901a\u8fc7\u5355\u72ec\u7684unicode()\u51fd\u6570\u8f6c\u6210unicode\u7c7b\u578b\uff0c\u4f46\u6ca1\u6709byte\u7c7b\u578b\u3002
\u800c\u5728Python 3\u4e2d\uff0c\u7ec8\u4e8e\u6709\u4e86Unicode\uff08utf-8\uff09\u5b57\u7b26\u4e32\uff0c\u4ee5\u53ca\u4e24\u4e2a\u5b57\u8282\u7c7b\uff1abytes\u548cbytearrays\u3002Python 3\u53ea\u6709\u4e00\u79cd\u7c7b\u578b\uff1aUnicode\u5b57\u7b26\u4e32(Unicode strings)\u3002\u53ea\u6709str\u4e00\u79cd\u7c7b\u578b\uff0c\u4f46\u5b83\u8ddf2.x\u7248\u672c\u7684unicode\u51e0\u4e4e\u4e00\u6837\u3002

Notes
Python 2
Python 3


\u2460
u'PapayaWhip' 'PapayaWhip'

\u2461
ur'PapayaWhip\foo' r'PapayaWhip\foo'
Python 2\u91cc\u7684Unicode\u5b57\u7b26\u4e32\u5728Python 3\u91cc\u5373\u666e\u901a\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u5728Python 3\u91cc\u5b57\u7b26\u4e32\u603b\u662fUnicode\u5f62\u5f0f\u7684\u3002
Unicode\u539f\u59cb\u5b57\u7b26\u4e32(raw string)(\u4f7f\u7528\u8fd9\u79cd\u5b57\u7b26\u4e32\uff0cPython\u4e0d\u4f1a\u81ea\u52a8\u8f6c\u4e49\u53cd\u659c\u7ebf"\")\u4e5f\u88ab\u66ff\u6362\u4e3a\u666e\u901a\u7684\u5b57\u7b26\u4e32\uff0c\u56e0\u4e3a\u5728Python 3\u91cc\uff0c\u6240\u6709\u539f\u59cb\u5b57\u7b26\u4e32\u90fd\u662f\u4ee5Unicode\u7f16\u7801\u7684\u3002
\u5168\u5c40\u51fd\u6570unicode()
Python 2\u6709\u4e24\u4e2a\u5168\u5c40\u51fd\u6570\u53ef\u4ee5\u628a\u5bf9\u8c61\u5f3a\u5236\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\uff1aunicode()\u628a\u5bf9\u8c61\u8f6c\u6362\u6210Unicode\u5b57\u7b26\u4e32\uff0c\u8fd8\u6709str()\u628a\u5bf9\u8c61\u8f6c\u6362\u4e3a\u975eUnicode\u5b57\u7b26\u4e32\u3002
Python 3\u53ea\u6709\u4e00\u79cd\u5b57\u7b26\u4e32\u7c7b\u578b\uff0cUnicode\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5str()\u51fd\u6570\u5373\u53ef\u5b8c\u6210\u6240\u6709\u7684\u529f\u80fd\u3002(unicode()\u51fd\u6570\u5728Python 3\u91cc\u4e0d\u518d\u5b58\u5728\u4e86\u3002)

Notes
Python 2
Python 3



unicode(anything) str(anything)
5.\u6570\u636e\u7c7b\u578b
1\uff09Python 2\u6709\u4e3a\u975e\u6d6e\u70b9\u6570\u51c6\u5907\u7684int\u548clong\u7c7b\u578b\u3002int\u7c7b\u578b\u7684\u6700\u5927\u503c\u4e0d\u80fd\u8d85\u8fc7sys.maxint\uff0c\u800c\u4e14\u8fd9\u4e2a\u6700\u5927\u503c\u662f\u5e73\u53f0\u76f8\u5173\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u6570\u5b57\u7684\u672b\u5c3e\u9644\u4e0a\u4e00\u4e2aL\u6765\u5b9a\u4e49\u957f\u6574\u578b\uff0c\u663e\u7136\uff0c\u5b83\u6bd4int\u7c7b\u578b\u8868\u793a\u7684\u6570\u5b57\u8303\u56f4\u66f4\u5927\u3002
\u5728Python 3\u91cc\uff0c\u53ea\u6709\u4e00\u79cd\u6574\u6570\u7c7b\u578bint\uff0c\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5b83\u5f88\u50cfPython 2\u91cc\u7684\u957f\u6574\u578b\u3002
Note:\u68c0\u67e5\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u662f\u6574\u578b\uff0c\u83b7\u5f97\u5b83\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5e76\u4e0e\u4e00\u4e2aint\u7c7b\u578b(\u4e0d\u662flong)\u7684\u4f5c\u6bd4\u8f83\u3002\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528isinstance()\u51fd\u6570\u6765\u68c0\u67e5\u6570\u636e\u7c7b\u578b\uff1b\u518d\u5f3a\u8c03\u4e00\u6b21\uff0c\u4f7f\u7528int\uff0c\u800c\u4e0d\u662flong\uff0c\u6765\u68c0\u67e5\u6574\u6570\u7c7b\u578b\u3002
sys.maxint
\u7531\u4e8e\u957f\u6574\u578b\u548c\u6574\u578b\u88ab\u6574\u5408\u5728\u4e00\u8d77\u4e86\uff0csys.maxint\u5e38\u91cf\u4e0d\u518d\u7cbe\u786e\u3002\u4f46\u662f\u56e0\u4e3a\u8fd9\u4e2a\u503c\u5bf9\u4e8e\u68c0\u6d4b\u7279\u5b9a\u5e73\u53f0\u7684\u80fd\u529b\u8fd8\u662f\u6709\u7528\u5904\u7684\uff0c\u6240\u4ee5\u5b83\u88abPython 3\u4fdd\u7559\uff0c\u5e76\u4e14\u91cd\u547d\u540d\u4e3asys.maxsize\u3002

Notes
Python 2
Python 3


\u2460
from sys import maxint from sys import maxsize

\u2461

python中python2和python3之间的转换方式如下:

一、放弃python 2.6之前的python版本

python 2.6之前的python版本缺少一些新特性,会给你的迁移工作带来不少麻烦。如果不是迫不得已还是放弃对之前版本的支持吧。

二、使用 2to3 工具对代码进行检查和转换

2to3是python自带的一个代码转换工具,可以将python2的代码自动转换为python3的代码。当然,不幸的是转换出的代码并没有对python2的兼容做任何的处理。所以我们并不真正使用2to3转换出的代码。执行2to3.py 查看输出信息,并修正相关问题。为了简化这个转换过程,这个脚本会将你的Python 2程序源文件作为输入,然后自动将其转换到Python 3的形式。



这简单!

访问网页链接这个网站就可以了!又自动转换的站点。

请注意!python2和python3是两种不同的语言,只是都属于python而已,所以不要自己直接改,相信电脑!



  • Python鐨杩欏嚑涓増鏈尯鍒
    绛旓細绯荤粺鍖哄埆锛1銆丏ownload Windows X86 鍙緵鐢佃剳绯荤粺32浣嶄娇鐢ㄣ2銆丏ownload Windows X86-64 鍙緵鐢佃剳绯荤粺64浣嶄娇鐢ㄣ傜増鏈尯鍒細1銆丏ownload Windows X86 web-based installer 鏄氳繃闇瑕佽仈缃戝畬鎴愬畨瑁鐨鏂囦欢銆2銆丏ownload Windows X86 executable installer 鏄彲鎵ц鐨勫畨瑁呮枃浠讹紝涓嬭浇鍚庯紝鍙屽嚮瀹夎鍗冲彲銆3銆丏ownload ...
  • python2鍜3鐨勫尯鍒ぇ鍚?
    绛旓細1銆丳ython2涔嬪墠鏄潪甯稿彈娆㈣繋鐨勶紝鐜板湪寰堝杞欢鍏徃渚濈劧浣跨敤鐨勬槸Python2锛屼笉杩囩敱浜庤秺鏉ヨ秺澶氱殑鍏徃寮濮嬩粠Python2杩佺Щ鍒3锛屽洜姝ゅ垵瀛﹁呭彲浠ラ伩鍏嶈姳璐规洿澶氱殑鏃堕棿瀛︿範杩囧幓鐨勭増鏈簡銆2銆Python2鍜孭ython3涔嬮棿鏈変竴浜涙槸鏃犳硶浜掔浉鍏煎鐨勶紝鐜板湪寰堝寮鍙戜汉鍛橀兘鍦ㄤ娇鐢≒ython3寮鍙戝簱锛岃澶氳佺殑鍩轰簬Python2鐨勫紑鍙戝寘渚濈劧鏄棤娉曞吋瀹...
  • 鐢python2杩樻槸python3
    绛旓細閫夋嫨 閭f槸瀛︿範 Python2 杩樻槸 Python 3鍛紵鐧芥湀榛戠窘璁や负锛氬鏋滀綘鏄竴涓垵瀛﹁咃紝鐧芥湀榛戠窘寤鸿浣犲彲浠 鐩存帴瀛︿範 Python 3锛屼粠杩欓噷寮濮嬪拰鐧芥湀榛戠窘涓璧峰涔燩ython3鍚!濡傛灉浣犳槸涓涓狿ython2 鐨勫紑鍙戣咃紝鐧芥湀榛戠窘涔熷缓璁厛浜嗚ВPython2鍜孭ython3鐨勫尯鍒紝浠ヤ究鎶婁綘鐨勪骇鍝佹棭鏃ュ崌绾ф敮鎸丳ython 3 Python3 鐩墠浣跨敤姣旇緝...
  • 鏁欎綘濡備綍鍦╓indows鐜涓嬪畨瑁python2鍜宲ython3涓や釜鐗堟湰
    绛旓細杩欑瘒鏂囩珷涓昏浠嬬粛涓媁indows锛堟垜鐢ㄧ殑Win10锛夌幆澧冧笅鐨刾ython2.x 鍜 python3.x 鐨勫畨瑁咃紝浠ュ強python2.x 涓 python3.x 鍏卞瓨鏃剁殑閰嶇疆闂銆傜幇鍦ㄥぇ瀹跺父鐢ㄧ殑妗岄潰鎿嶄綔绯荤粺鏈夛細Windows銆丮ac OS銆乽buntu锛屽叾涓璏ac OS 鍜 ubuntu涓婇兘浼氳嚜甯ython銆傝繖閲屾垜浠彧浠嬬粛涓媁indows锛堟垜鐢ㄧ殑Win10锛夌幆澧冧笅鐨刾ython2.x 鍜 ...
  • python3鍜2鐨勫尯鍒湁浠涔
    绛旓細python2鐗堟湰鍜python3鐗堟湰鐨勫尯鍒紝涓よ呭姣斿涓 Python3 瀵 Unicode 瀛楃鐨勫師鐢熸敮鎸併侾ython2 涓娇鐢 ASCII 鐮佷綔涓洪粯璁ょ紪鐮佹柟寮忓鑷 string 鏈変袱绉嶇被鍨 str 鍜 unicode锛孭ython3 鍙 鏀寔 unicode 鐨 string銆Python2 鍜 Python3 瀛楄妭鍜屽瓧绗﹀搴斿叧绯讳负锛歅ython3 閲囩敤鐨勬槸缁濆璺緞鐨勬柟寮忚繘琛 import 鎺ㄨ崘锛...
  • python2鍜宲ython3鐨勫尯鍒
    绛旓細> 3. 鍘熸潵1/2锛堜袱涓暣鏁扮浉闄わ級缁撴灉鏄0锛岀幇鍦ㄦ槸0.5浜 python 2.2+ 浠ヤ笂閮藉彲浠ヤ娇鐢 from __future__ import division 瀹炵幇鏀圭壒鎬, 鍚屾椂娉ㄦ剰 // 鍙栦唬浜嗕箣鍓嶇殑 / 杩愮畻 > 4. 鏂扮殑瀛楃涓叉牸寮忓寲鏂规硶format鍙栦唬 閿欒, 浠 python2.6+ 寮濮嬪凡缁忓湪str鍜寀nicode涓湁璇ユ柟娉, 鍚屾椂 python3渚濈劧鏀寔 ...
  • 鍦╳indows涓嬪悓鏃跺畨瑁python2鍜宲ython3寮鍙戠幆澧
    绛旓細瀛ython杩樺湪绾犵粨鐢╬ython2杩樻槸python3锛熶笉濡備竴璧峰畨瑁呭緱浜 绗竴姝ワ紝鍘籶ython瀹樼綉 python.org 涓嬭浇瀹樻柟瀹夎鍖咃紝閫夋嫨python2鍜宲ython3鐨勭増鏈 绗簩姝ワ紝涓嬭浇鏃跺彲浠ュ彂鐜皃ython2鐨勭増鏈槸msi瀹夎鍖咃紝python3鏄痚xe瀹夎鍖咃紝涓嬭浇瀹屾垚鍚庣洿鎺ュ弻鍑诲畨瑁呭嵆鍙傛敞鎰忓厛瀹夎python3锛屽苟涓斿嬀閫夊簳閮ˋdd to path閫夐」銆傜敱浜庢垜宸茬粡...
  • python2鍜宲ython3鐨勫尯鍒
    绛旓細python2鍜python3鐨勫尯鍒 濡傛灉浣犳槸涓涓垵瀛﹁咃紝鎴栬呬綘浠ュ墠鎺ヨЕ杩囧叾浠栫殑缂栫▼璇█锛屼綘鍙兘涓嶇煡閬擄紝鍦ㄥ紑濮嬪涔python鐨鏃跺欓兘浼氶亣鍒颁竴涓瘮杈冭浜哄緢澶寸柤鐨勯棶棰橈細鐗堟湰闂锛侊紒鏄涔爌ython2 杩樻槸瀛︿範 python3 锛熻繖鏄潪甯歌浜虹籂缁撶殑锛佹悳绱竴涓嬩究浼氬彂鐜皃ython3 鍜 python2 鏄笉鍏煎鐨勶紝鑰屼笖宸紓姣旇緝澶э紝鍒板簳瀛︿範...
  • 鈥python2鈥濆拰鈥減ython3鈥濇湁浠涔堝尯鍒?
    绛旓細涔嬪墠涓や釜鐗堟湰鍏卞瓨鍙槸鍥犱负璇稿package鐨勬洿鏂版崲浠e苟娌℃湁璺熶笂锛屼絾鏄埌浜嗙幇鍦ㄥ彧瑕佽繕鏈夋椿浜虹淮鎶ょ殑package鍩烘湰閮藉吋瀹3浜嗭紝鐒惰屾柊寤虹殑package鍗磋秺鏉ヨ秺澶氫笉鍏煎2銆備娇鐢2鐨勬剰涔夊熀鏈病鏈変簡锛屾棩甯哥敤3澶т笉浜嗙暀涓涓2澶囩敤灏辫銆傛嫇灞曞洖绛旓細Python, 鏄竴绉嶉潰鍚戝璞$殑瑙i噴鍨嬭绠楁満绋嬪簭璁捐璇█锛岀敱鑽峰叞浜篏uido van Rossum浜...
  • python涓2鍜2.0鐩哥瓑鍚?
    绛旓細濡傛灉鏄暟瀛楀瀷鏁版嵁锛2鍜2.0鐩哥瓑锛屽鏋滄槸瀛楃鍨嬫暟鎹紝閭e氨涓嶇浉绛
  • 扩展阅读:photoshop永久免费版 ... photoshop在线网页版 ... pubmed官方官网入口 ... python中2**2**3 ... wordpress免费网站 ... python3.6下载官网 ... python for beginners ... python手机版下载安装 ... x%2 0 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网