作家
登录

你需要知道的、有用的Python功能和特点

作者: 来源: 2017-06-06 12:02:46 阅读 我要评论

  •  
  • # I1 
  •  
  • aS'two' 
  •  
  • # p3 
  •  
  • # aaS'apple' 
  •  
  • # p4 
  •  
  • # a. 
  •  
  • # ['hello', 42, [1, 'two'], 'apple' 
  • 这是一个原生的Python序列化办法。然而近几年来JSON变得风行起来,Python添加了对它的支撑。如今你可以应用JSON来编解码。

    1. import json 
    2.  
    3.   
    4.  
    5. variable = ['hello', 42, [1,'two'],'apple'
    6.  
    7. print "Original {0} - {1}".format(variable,type(variable)) 
    8.  
    9.   
    10.  
    11. # encoding 
    12.  
    13. encode = json.dumps(variable) 
    14.  
    15. print "Encoded {0} - {1}".format(encode,type(encode)) 
    16.  
    17.   
    18.  
    19. #deccoding 
    20.  
    21. decoded = json.loads(encode) 
    22.  
    23. print "Decoded {0} - {1}".format(decoded,type(decoded)) 
    24.  
    25.   
    26.  
    27. output 
    28.  
    29.   
    30.  
    31. # Original ['hello', 42, [1, 'two'], 'apple'] - <type 'list'=""
    32.  
    33. # Encoded ["hello", 42, [1, "two"], "apple"] - <type 'str'=""
    34.  
    35. # Decoded [u'hello', 42, [1, u'two'], u'apple'] - <type 'list'="" 

    如许更紧凑,并且最重要的是如许与JavaScript和很多其他说话兼容。然而对于复杂的对象,个中的一些信息可能损掉。

    你可能会留意到,即使字符串是独一的,但它们河畔的几个字符看起来很类似。这是因为生成的字符串与电脑的MAC地址是相接洽的。

    紧缩字符

    当谈起紧缩时我们平日想到文件,比如ZIP构造。在Python中可以紧缩长字符,不涉及任何档案文件。

    1. import zlib 
    2.  
    3.   
    4.  
    5. string =  """   Lorem ipsum dolor sit amet, consectetur 
    6.  
    7.                 adipiscing elit. Nunc ut elit id mi ultricies 

        推荐阅读

        浅谈Web自适应

      媒介<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> 简单工作简单做-宽度自适应所谓宽度自适应严格来说是一种PC端的自适>>>详细阅读


      本文标题:你需要知道的、有用的Python功能和特点

      地址:http://www.17bianji.com/lsqh/35585.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)