大家好,小编为大家解答列举 python2和python3的区别?的问题。很多人还不知道python2和python3的主要区别,现在让我们一起来看看吧!
目录
1、数字处理不同--py3除法得float,py2看父母
(1)、py2中有long int,py3没有,这个不多说了
除法:python3如何都是float结果,py2父母都是整型,结果就是整型小数点后的都不要了
2、python3增加bytes类型类型
3、time.time()不同
time.time()精度不一样
4、print和repr
python2的repr方法和print不一致了,python3处理一致
5、编码方式区别Unicode
大家都知道的区别,我介绍下我遇到问题的处理
(1)json就怕遇到这样的:
json.dumps()
json.loads()
6、包名不同
python2 :Queue python3改名:queue
ConfigParser configparser
SocketServer socketserver
7、不等运算符
python2:两种 != 和<>
python3:只有!=
8、python3异常处理不同
9、xrange不同
10、map、filter 和 reduce区别
11、urlencode和urldecode不同
(1)python2处理
包urllib中 urlencode、quote、unquote
操作的url中有中文需要注意unicode、str的编码格式
# urlencode
# 对json数据类型urlencode
from urllib import urlencode
data = {
'a': 'test',
'name': '魔兽'
}
f = urlencode(data)
print f
# 对字符串urlencode
from urllib import quote
w = quote('魔兽')
print w
# urldecode
from urllib import unquote
print unquote(f)
# 结果:a=test&name=魔兽
print unquote(w)
# 实际应用
# url = “http://ceshi/hello/321403美康利健红光坐灸治疗仪-DM.mp4”
# 将 dict = {“Path”: quote(url)}保存在文件A.txt中
import json
with open("A.txt","r") as t:
temp = json.loads(t.read())
print(type(temp))
for i in temp:
print(type(i['Path'])) #发现这里是unicode类型
print(unquote(i["Path"].encode("utf-8"))) # 获取的url正确
(2)python3处理
包urllib中 parse
from urllib import parse #一定要这样引包,要不报错
test = "微信小程序"
encode = urllib.parse.quote(test)
print(encode)
decode = urllib.parse.unquote(encode)
print(decode)
12、subprocess包不同
(1)python2中没有run方法
(2)python2中没有getoutput方法
(3)python2中没有getstatusoutput方法
(4)python会出现子命令没有执行完就向下执行代码(问题2)
subprocess包常用的方法:Popen、call、check_call 、check_output
这里简单介绍下这个包中参数特点:
subprocess.run("ping 10.111.6.231",shell=True)
shell参数:按照shell执行,如果没有这个参数在liunx执行报错
这里ping语句会一直打印过程日志,不会结束,占着主进程,类似的还有top,ffmpeg播放音视频等,这是问题1
不同方法返回类型不同:有返回命令执行状态码(call、check_out、Popen,不会等命令执行完成的,wget一个大文件你就发现了),有返回执行输出的(带output都是要获取执行输出的,会等命令执行完成的) 问题2(这个问题在python2上有问题,python3没有问题)
stdout=subprocess.PIPE:获取标准输出
stderr=subprocess.PIPE:获取错误输出
问题1:
# -*- coding:utf-8 -*-
import os
import subprocess
from threading import Timer
from signal import SIGKILL
class MyTimeOut(object):
def __init__(self):
self.stdout = []
self.stderr = []
self.timeout = 10
self.is_timeout = False
def timeout_callback(self, p):
self.is_timeout = True
print('timeout')
print(p.pid)
try:
os.killpg(p.pid, SIGKILL)
except Exception as error:
print(error)
def run(self, shellStr, logPath):
p = subprocess.Popen(shellStr, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid,
cwd=logPath)
this_timer = Timer(self.timeout, self.timeout_callback, [p])
this_timer.start()
try:
print("开始执行; 超时时间为 %d \n" % self.timeout)
stdout, stderr = p.communicate()
exit_code = p.returncode
print(exit_code)
print(stdout.decode("gbk"))
print(stderr.decode("gbk"))
return stdout
finally:
this_timer.cancel()
if __name__=='__main__':
n = MyTimeOut()
n.run("ping 10.111.6.231","./")
问题2:
# 使用获取运行输出的方法,只在python2有这个问题
# 希望wget下载完文件后在操作其他:下面截图看11111的打印就知道wget是否执行完就向下运行了呢
# -*- coding:utf-8 -*-
import subprocess
cmd = "wget 你找到一个可下载的地址"
subprocess.check_output(cmd,shell=True)
# subprocess.Popen(cmd,shell=True)
print("11111111111111")