• 实战篇一 python常用模块和库介绍


    # -_-@ coding: utf-8 -_-@ 
    
    -- Python 常用模块和库介绍  
    
    第一部分:json模块介绍 
    
    import json  
    
    将一个Python数据结构转换为JSON:  
    dict_ = {1:2, 3:4, "55":"66"}  
    # test json.dumps  
      
    print type(dict_), dict_  
    json_str = json.dumps(dict_)  
    print "json.dumps(dict) return:"  
    print type(json_str), json_str  
    
    # json.dumps结果是字符串  
    
    将一个JSON编码的字符串转换回一个Python数据结构
    
    # test json.loads  
    print "
    json.loads(str) return"  
    dict_2 = json.loads(json_str)  
    print type(dict_2), dict_2  
    
    # json.dumps结果是字典 
    
    
    json.dumps : dict转成str
    json.loads:str转成dict
    
    
    以下信息原始地址:http://www.cnblogs.com/linjiqin/p/3674745.html  
    
    #-*-coding:utf-8-*- 
    
    '''编码格式记得统一,不然容易出现中文乱码,推荐用utf-8'''
    
    import json
    
    ##################json单对象##############################
    #声明初始化一个变量
    obj={'name':'张三', 'email':'ljq@gmail.com'}
    print type(obj) #<type 'dict'>
    
    #字典转为json
    #json_obj=json.dumps(obj) 
    json_obj=json.dumps(obj, ensure_ascii=False) #解决中文乱码
    print type(json_obj) #<type 'str'>
    print json_obj #{"name": "ljq", "email": "ljq@gmail.com"}
    
    #字符串转为json
    str="{'name':'李四', 'email':'lisi@gmail.com'}"
    #json_str=json.dumps(str)
    json_str=json.dumps(str, ensure_ascii=False)
    print json_str #"{'name':'张安', 'email':'ljq@gmail.com'}"
    
    #json转为字典
    dict_obj=json.loads(json_obj)
    #如果传入的对象的编码不一致的话,需要用encoding指定字符编码
    #dict_obj=json.loads(json_obj, encoding="gbk") 
    print type(dict_obj) #<type 'dict'>
    print dict_obj.get('name','')
    
    #json转为字符串
    unicode_str=json.loads(json_str)
    #unicode_str=json.loads(json_str,encoding = "gbk") 
    dict_str=eval(unicode_str)
    print type(unicode_str) #<type 'unicode'>
    print type(dict_str)
    print dict_str.get('name', '') ## 如果是打印出来是乱码, 那就是原来的xe6x9dx8exe6x80x9d 是 utf8格式的, 先要解码, 然后再编码展示 
    
    
    >>> print dict_str
    {'name': 'xe6x9dx8exe6x80x9d', 'email': 'lisi@gmail.com'}
    print dict_str.get('name','').decode("utf8").encode("gbk")
    print dict_str.get('name','').decode("utf8").encode("gb2312")
    
    
    
    
    
    ##################json数组##############################
    arrays=[
        {'name':'zhangsan', 'age':10},
        {'name':'lisi', 'age':20},
        {'name':'wangwu', 'age':30}
    ]
    
    arrays_json=json.dumps(arrays)
    print type(arrays_json) #<type 'str'>
    
    for obj in json.loads(arrays_json):
        #print type(obj) #<type 'dict'>
        print obj.get('age', '')
    
    ##################django内置json---simplejson##############################
    '''
    import simplejson as json
    
    #几个主要函数:dump、dumps、load、loads,带s跟不带s的区别: 带s的是对 字符串的处理,而不带 s的是对文件对像的处理。
    
    json、simplejson效率比较:simplejson在效率上来得有优势,推荐用simplejson
    
    '''
    
    
    
    第二部分:urllib库介绍 
    
    import urllib 
    
    # Python urllib 库提供了一个从指定的 URL 地址获取网页数据,然后对其进行分析处理,获取想要的数据
    
    urllib.urlopen(url[,data[,proxies]])   打开一个URL的方法, 返回一个 "文件对象" 
    
    urlopen 返回对象的方法主要有: 
    
    1. read(),readline(),readlines(),fileno(), close()
    2. info(): 返回一个httplib.HTTPMessage对象, 远程服务器返回的头信息
    3. getcode(): 返回http状态吗 
    4. geturl(): 返回请求的URL  
    
    
    重点实践 readlines(), info() 事件 
    
    f = urllib.urlopen('http://www.zhihu.com/')
    firstline = f.readline() # 读取HTML的第一行 
    firstLine 
    
    read() 将文件内容全部读出来放入一个而变量中,类型为字符串.
    readline() 每次读一行,保存在一个变量中,类型为字符串.
    readlines() 一次将全部的行读出来, 保存为一个列表,类型为列表.  
    
    
    
    
    第三部分: 解码和编码介绍 (decode 、 encode 介绍 )
    
    字符串在Python内部的表示是unicode编码,
    通常需要以unicode作为中间编码,
    即先将其他编码的字符串解码(decode)成unicode,
    再从unicode编码(encode)成另一种编码 
    
    decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串转换成unicode编码。
    
    encode的作用是将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串转换成gb2312编码。
    
    来源 http://www.cnblogs.com/linjiqin/p/3674745.html   
    
    s = '中文'
    print type(s)
    print s 
    
    s1 = s.decode('utf8')   #解码utf8,默认的编码方式是unicode 
    print s1
    
    s2 = s.decode('utf8')  #解码utf8 
    print s2 
    s3 = s.decode('utf8','ignore') #解码utf8,忽略其中有异常的编码,仅显示有效的编码 
    s3 = s.decode('utf8','replace')
    print s3 
    
    
    s4 = s1.encode('gb2312')   ##编码为utf8
    print type(s4)
    print s4 
    
    从unicode转str,被看做是把一个信息文本编码为二进制字节流的过程,要用encode方法
    
    
    
    第四部分:types模块 
    
    
    第五部分:创建一个类  
  • 相关阅读:
    cocos2dx ScrollView 测试一 触摸事件优先级和自动调整
    cocos2dx cpp与oc混编打开ios摄像头或图库取图
    cocos2dx 触摸测试四 Armature
    cocos2dx 触摸测试三 优先级及阻止传递
    cocos2dx 触摸测试二 多点包含单点
    cocos2dx 触摸测试一 单点和多点
    cocos2dx xcode5 创建项目
    ORACLE 数据的逻辑组成
    ORACLE rowid,file# 和 rfile#
    ORACLE object_id和data_object_id
  • 原文地址:https://www.cnblogs.com/huiming/p/6006362.html
Copyright © 2020-2023  润新知