python基础知识---模块,,import day


技术分享
import day5.lib.commands           #导入模块r=day5.lib.commands.testmodule()print(r)from day5.lib.m1 import logout as m1_logout   #导入某个模块的函数r=m1_logout()import sysprint(sys.argv)
View Code

#安装第三方模块方法:
#1,pip命令安装
# 如果报不是内部或者外部命令,则是环境变量问题,找到python安装目录,使用绝对路径执行pip命令
#C:\Users\zhoufeng\AppData\Local\Programs\Python\Python35\Scripts\pip install requests

#2,源码安装


二、json与pickle模块
json模块用于字符串与python基本数据类型(列表、元组、字典等等)的转换,因为字符串是各语言都有的数据类型,所以json用于各语言间传递数据
pickle模块用于任何python对象的序列化与反序列化

技术分享
import json##json有四种方法:dumps  dump   loads   loaddic={‘k1‘:‘v1‘}print(dic,type(dic))result=json.dumps(dic)           #json.dumps()将python基本数据类型转换成字符串print(result,type(result))s1=‘{"k1":123}‘dic=json.loads(s1)     #json.loads()将字典形式的字符串转换成python字典,列表形式的字符串转换成python列表print(dic,type(dic))li=[11,22,33]json.dump(li,open(‘db‘,‘w‘))           #dumps直接在内存中完成转换  dump可以用于写文件li=json.load(open(‘db‘,‘r‘))           #loads直接在内存中完成转换,load可以用于读文件print(type(li),li)lst="[‘alex‘,‘zhoufeng‘]"ret=json.loads(lst)print(type(ret))##########################################import pickleli=[11,22,33]r=pickle.dumps(li)print(r)result=pickle.loads(r)print(result)li=[11,22,33]pickle.dump(li,open(‘db‘,‘wb‘))#json与pickle的区别:#json与pickle都有四种方法#json更加适合跨语言     但只适用于基本的数据类型(字典、列表、元组)#pickle仅适用于python    但可用于python所有类型的序列化
View Code
技术分享
import pickleli=(11,22,33)r=pickle.dumps(li)print(r)#pickle.dumps用法:字符串=pickle.dumps(序列)li_new=pickle.loads(r)print(li_new)#pickle.loads用法:序列=pickle.loads(字符串)
View Code技术分享
import  pickleclass teacher:    def __init__(self,name,age,asset,coursename):        self.name=name        self.age=age        self.asset=asset        self.coursename=coursenameteacherlist=[]teacher1=teacher(‘zhoufeng‘,18,10,‘shuxue‘)teacher2=teacher(‘malulu‘,18,10,‘zhongwen‘)teacher3=teacher(‘zhoufang‘,19,20,‘yingyu‘)teacherlist.append(teacher1)teacherlist.append(teacher2)teacherlist.append(teacher3)with open(‘test1.txt‘,‘wb‘) as file_obj1:       #注意文件名带后缀    pickle.dump(teacherlist,file_obj1)#pickle.dump用法:pickle.dump(序列,文件对象)with open(‘test1.txt‘,‘rb‘) as file_obj2:    li=pickle.load(file_obj2)#pickle.load用法:序列=pickle.load(文件对象)print(li)
View Code

三、requests模块
技术分享
#import requests#import json#response=requests.get(‘http://wthrcdn.etouch.cn/weather_mini?city=北京‘)#response.encoding=‘utf-8‘#print(response.text)#dic=json.loads(response.text)#print(type(dic))
View Code

四、与系统操作相关的模块




五、string模块

translate()与maketrans()
maketrans("abe","ABE") a-->A b-->B e-->E 生成转换关系表
translate(arg1,arg2) arg1是maketrans函数生成的转换关系表,arg2可以没有,表示需要删除的字符

示例1:‘A‘与‘T‘对应 ‘C‘与‘G‘对应 互相转换 输入"AATTC" 输出"TTAAG"
import string         #python3中不需要导入def DNA_strand(dna):    return dna.translate(string.maketrans("ATCG","TAGC"))

示例2: 先删除掉‘123‘ 然后将‘a‘-->‘A‘ ‘b‘-->‘B‘ ‘c‘-->‘C‘

>>> import string                  >>> t=string.maketrans(‘abc‘,‘ABC‘)>>> ‘abc123‘.translate(t,‘123‘)    ‘ABC‘

六、collections模块

Counter()

对序列中的元素进行统计,并以字典的形式返回,而且还能对集合之间求差集

技术分享
>>> from collections import Counter>>> string1="aaabb">>> print Counter(string1)Counter({‘a‘: 3, ‘b‘: 2})>>> >>> string2="bbccc">>> print Counter(string2)Counter({‘c‘: 3, ‘b‘: 2})>>> >>> print Counter(string1)-Counter(string2)   #返回string1与string2的差(string1中有但string2中没有的部分)Counter({‘a‘: 3})>>> >>> string3="aabb">>> print Counter(string3)Counter({‘a‘: 2, ‘b‘: 2})>>> print Counter(string1)-Counter(string3)  #返回string1与string3的差Counter({‘a‘: 1})
View Code



python基础知识---模块

相关内容

    暂无相关文章

评论关闭