zoukankan      html  css  js  c++  java
  • 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 1in <module>
      File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 312in loads
        s.__class__.__name__))
    TypeError: the JSON object must be strnot '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"))

      

  • 相关阅读:
    redis搭建集群
    redis搭建主从
    redis与python交互
    redis数据操作篇
    redis配置篇
    node 淘宝镜像
    java 深copy
    springmvc配置访问静态文件
    centos 启动 oracle
    List 分隔多次执行 且在同一个事物当中
  • 原文地址:https://www.cnblogs.com/whnbky/p/11424546.html
Copyright © 2011-2022 走看看