Python2到python3的轉換

  1. subprocess返回的output中有\n
  2. bytes到str的轉換
    參考: https://blog.csdn.net/weixin_40283816/article/details/83591582
b = b"example"    # bytes object  
s = "example"     # str object  
sb = bytes(s, encoding = "utf8")    # str to bytes 
或者:sb = str.encode(s)             # str to bytes 
bs = str(b, encoding = "utf8")      # bytes to str  
或者:bs = bytes.decode(b)           #  bytes to str  

  1. urllib和urllib2的轉換
    參考: https://blog.csdn.net/python36/article/details/84568183
Py2.x:
 
Urllib庫
Urllin2庫
Py3.x:
 
Urllib庫
變化:
 
在Pytho2.x中使用import urllib2——-對應的,在Python3.x中會使用import urllib.request,urllib.error。
在Pytho2.x中使用import urllib——-對應的,在Python3.x中會使用import urllib.request,urllib.error,urllib.parse。
在Pytho2.x中使用import urlparse——-對應的,在Python3.x中會使用import urllib.parse。
在Pytho2.x中使用import urlopen——-對應的,在Python3.x中會使用import urllib.request.urlopen。
在Pytho2.x中使用import urlencode——-對應的,在Python3.x中會使用import urllib.parse.urlencode。
在Pytho2.x中使用import urllib.quote——-對應的,在Python3.x中會使用import urllib.request.quote。
在Pytho2.x中使用cookielib.CookieJar——-對應的,在Python3.x中會使用http.CookieJar。
在Pytho2.x中使用urllib2.Request——-對應的,在Python3.x中會使用urllib.request.Request.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章