Python3.x与Python2.x的差异用法

1,关于urllib2区别:

# python2
import urllib2 # python3
# 用urllib.request代替urllib2
import urllib.request

2,关于URLError输出用法区别:

# python2
urllib2.URLError, e: # python3
urllib.request.URLError as e:

3,关于print用法区别:

# python2
print 'hello world' # python2
print('hello world')

4,关于aw_input和input区别:

# python2
#有raw_input和input # python3
#将raw_input和input进行了整合,只有input

5,关于设置setdefaultencoding区别:

# python2
reload(sys)
sys.setdefaultencoding("utf-8") # python3
#python3字符串默认编码unicode, 所以sys.setdefaultencoding也不存在,不然会报错:AttributeError: module 'sys' has no attribute 'setdefaultencoding'
#python3.3(低于3.3版本):
import imp
imp.reload(sys)
#python3.4(高于3.4版本):
import importlib
importlib.reload(sys)

6,关于urlretrieve()的区别:

# python2
#直接将远程数据下载到本地。
urllib.urlretrieve(url[, filename[, reporthook[, data]]])
参数说明:
url:外部或者本地url
filename:指定了保存到本地的路径(如果未指定该参数,urllib会生成一个临时文件来保存数据);
reporthook:是一个回调函数,当连接上服务器、以及相应的数据块传输完毕的时候会触发该回调。我们可以利用这个回调函数来显示当前的下载进度。
data:指post到服务器的数据。该方法返回一个包含两个元素的元组(filename, headers),filename表示保存到本地的路径,header表示服务器的响应头。 # python3
urllib.request.urlretrieve()
#例子:
urllib.request.urlretrieve(imgurl,'d:\\%s.jpg'% x)

7,关于range()和xrange()区别:

# python2
#有range()和xrange()两个
for x in xrange(1,10,2):
if x==5:
continue
print(x)
for x in range(1,10,2):
if x==5:
continue
print(x) # python3
#将range()、xrange()两个合并为range()
for x in range(1,10,2):
if x==5:
continue
print(x)

 注意:Python3.x中将range()和xrang()合并为rang();

8,关于zip()函数的区别:

#python2
#zip()用法
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
xyz = zip(x, y, z)
print(xyz )
u = zip(*xyz)
print(u)
 
#python3
#zip()用法
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
xyz = list(zip(x, y, z))
print(xyz )
u = list(zip(*xyz))
print(u)

9,关于字Unicode字符串区别:

#python2
#Unicode字符串
u'PapayaWhip'
#Unicode原始字符串(使用这种字符串,python不会自动转义反斜线"\")也被替换为普通的字符串
ur'PapayaWhip\foo'
 
#python3
#python2中的Unicode字符串在python3即为普通字符串
'PapayaWhip'
#python3里,所有原始字符串都是以unicode编码的
r'PapayWhip\foo'

10,关于BeautifulSoup()中参数名称(fromEncoding)区别:

# python2
BeautifulSoup(start_html.content, "html.parser", fromEncoding="gb18030") #python3
BeautifulSoup(start_html.content, "html.parser", from_encoding="gb18030")

11,关于ConfigParser模块的用法:

#python2
import ConfigParser #python3
import configparser

关于python版本差异可以参考:https://www.cnblogs.com/weikunzz/p/6857971.html

Python3.x与Python2.x的差异用法的更多相关文章

  1. 自学Python1.3-centos内python3并与python2共存

    自学Python之路 自学Python1.3-centos内python3并与python2共存 1. 查看是否已经安装Python 测试机系统CentOS 7 默认安装了python2.7, 使用 ...

  2. Python3 join函数和os.path.join用法

    Python3  join函数和os.path.join用法 os.path.join()连接两个文件名地址的时候,就比os.path.join("D:\","test. ...

  3. Python3.x:bs4解析html基础用法

    Python3.x:bs4解析html基础用法 代码: import urllib.request from bs4 import BeautifulSoup import re url = r'ht ...

  4. 7-linux-Centos7安装python3并与python2共存

    转载自:https://www.cnblogs.com/JahanGu/p/7452527.html linux-Centos7安装python3并与python2共存   1.查看是否已经安装Pyt ...

  5. Python3.x:正则 re.findall()的用法

    Python3.x:正则 re.findall()的用法 概念: 语法:findall(pattern, string, flags=0) 说明:返回string中所有与pattern相匹配的全部字串 ...

  6. linux上安装python3同时保留python2

    linux上安装python3同时保留python2?这个就要用到上篇说到的path变量了. 具体介绍及操作 这里我下载python3.6版本来进行介绍 django默认数据库为sqlite3,所以安 ...

  7. Python3中内置类型bytes和str用法及byte和string之间各种编码转换,python--列表,元组,字符串互相转换

    Python3中内置类型bytes和str用法及byte和string之间各种编码转换 python--列表,元组,字符串互相转换 列表,元组和字符串python中有三个内建函数:,他们之间的互相转换 ...

  8. python3自动安装脚本,python3.x与python2.x共存

    1.前言: python3过程中,通过搜索一些文章参考安装过程发现比较麻烦,而且还出现一些不可预期的报错.python3环境需要升级openssl,所以为了部署到其他环境更方便,写自动安装脚本方式,且 ...

  9. 安装python3.8和python2.7

    在同一台电脑上同时安装Python2和Python3 目前Python的两个版本Python2和Python3同时存在,且这两个版本同时在更新与维护. 到底是选择Python2还是选择Python3, ...

随机推荐

  1. poj1001 Exponentiation【java大数】

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 183034   Accepted: 44062 ...

  2. element 表格元素 超链接

    1.在循环体中的事件绑定 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  3. TCP 123=网络时间协议(NTP),Net Controller

    TCP 123=网络时间协议(NTP),Net Controller

  4. Linux 下用 crontab 设置定时执行python 程序

    Linux 下用 crontab 设置定时执行python 程序 方法/步骤   1,先大概了解crontab,/etc/crontab 就是crontab 的配置文件. crontab命令详解可以查 ...

  5. 小希的迷宫---hdu1272

    http://acm.hdu.edu.cn/showproblem.php?pid=1272 #include<stdio.h> #include<string.h> #inc ...

  6. Distribution(F题)---第八届河南省程序设计大赛

    Description One day , Wang and Dong in the Dubai desert expedition, discovered an ancient castle. Fo ...

  7. Python性能鸡汤(转)

    英文原文:http://blog.monitis.com/index.php/2012/02/13/python-performance-tips-part-1/ 英文原文:http://blog.m ...

  8. 【Python】web.py初识学习

    简单而直接的Python web 框架:web.py 2016年11月03日 14:09:08 擒贼先擒王 阅读数:35157更多 个人分类: Web   From:https://www.oschi ...

  9. linux平台mysql密码设破解

    1.先停止mysql服务 service mysqld stop 2.启动mysql服务 并跳过权限认证 mysqld_safe --skip-grant-tables 3.打开另外一个终端 登录my ...

  10. http如何301到https呢?

    HTTPS协议的站点信息更加安全,同时可降低网站被劫持的风险,Firefox和chrome浏览器对访问一些非https站点会提示风险,BD等搜索引擎也明确表态了对https站点的友好.那么我们如何部署 ...