Python老师上课用2.7讲的。。。2.7能运行的代码到3.3里有些问题,顺手在这记下来。
1.
print()函数
2.7 可以不加括号
3.3 貌似必须加括号
20150208 补充:
print 在 python2 里是语句(statement),也是函数(since python 2.6)。
https://docs.python.org/2/reference/simple_stmts.html?#the-print-statement
https://docs.python.org/2/library/functions.html?highlight=print#print
print 在 python3 里不再是 statement,只是函数。
https://docs.python.org/3/reference/simple_stmts.html
https://docs.python.org/3/library/functions.html?highlight=print#print
2.
文件对象的构造
2.7 file()或open()
3.3 open()
3.
raw_input()函数
2.7 有
3.3 没有。“input()取代了raw_input(),eval(input())与原先的input()效果基本相同。”
来自
http://zhidao.baidu.com/question/428156750.html
4.
socket.send()
2.7 参数可以是字符串
3.3 “python3.2 socket.send 修改,传送数据必须是bytes”
http://zhidao.baidu.com/question/255087930.html
把socket实验程序从2.7搬到3.3折腾了好一会>_<
主要的点是:
1) 本文的3.提到的,raw_input()被input()取代;
2) socket.send()只能发bytes类型了。我这里转bytes类型时使用了utf-8编码:
tcpCliSock.send(bytes(oneStr,'utf-8'))
bytes类型转为String类型,要指定编码类型来进行解码,以下两种方式都可以:
3) 中文还是不正常啊……print (data.decode('utf-8')) #Or print (str(data,'utf-8'))