python3.5和3.6區別


 

python3.5和python3.6關於json模塊的區別

 

python3.5中

  無法反序列化bytes數據必須decode成str才可以

1
2
3
4
5
6
7
8
9
10
11
12
>>>  import  json
>>> a  =  b '{"username": "xxx"}'
>>> c  =  json.loads(a)
 
'''
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 312, in loads
     s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
 
'''

  3.5解決辦法:

1
2
3
4
5
6
7
8
9
10
>>> a  =  b '123'
>>> c  =  json.loads(a)
Traceback (most recent call last):
   File  "<stdin>" , line  1 in  <module>
   File  "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py" , line  312 in  loads
     s.__class__.__name__))
TypeError: the JSON  object  must be  str not  'bytes'
>>> c  =  json.loads(a.decode( 'utf-8' ))
>>> c
123

  

python3.6中

  無論bytes類型或者str類型都可以反序列化

1
2
3
4
5
>>>  import  json
>>> a  =  b '{"username": "xxx"}'
>>> c  =  json.loads(a)
>>> g  =  b '{"username": "xxx"}'
>>> h  =  json.loads(g.decode( "utf-8" ))

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM