Python 3.5 and 2.7 differentiate __python

Source: Internet
Author: User

1. Pickle Difference

In Python 2.7, the Dict object is stored serialized, and then free to serialize, with the following code in general:


However, when you invoke the above code in Python 3.5, you will report the following error:


This is the default for the 2.7pickle.load function read bytes read, while 3.5 read the default for ASCII read, the official website description is as follows:


So you just need to change the read format of the call to bytes.

Wind = pickle.load (pkl_file,encoding= ' bytes ')

2. URL access is different

In Python2.7, the request code for a URL is as follows:

Importurllib2
req = Urllib2. Request (' http://www.baidu.com ')
Response = Urllib2.urlopen (req)

Instead of using request in 3.5, the code is as follows:

From Urllibimportrequest

req = Request. Request (Req_url)

f= Request.urlopen (req)

And there is a more convenient way to access, you can avoid the lack of timely closure of the link, the code is as follows:

 from Import Request
 with  as F:
data = F.read ()
Print (' Status: ', F.status, F.reason)
 for  in F.getheaders ():
Print ('%s:%s '% (k, v))
Print (' Data: ', Data.decode (' Utf-8 '))

Access with AS: To automatically close links when page access is complete

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.