python(三),,数据类型和文件1.1


数据类型和文件

1.1 python字典

字典书写格式:

#!/usr/bin/env python#key-valueinfo={‘stu1101‘:"zhangsan",‘stu1102‘:"lizi",‘stu1103‘:"wangwu",}查询print(info)print("select-->",info["stu1101"])#names = info["stu1101"]#print(names)print("get-->",info.get("stu1102"))#get方法执行结果:{‘stu1103‘:‘wangwu‘,‘stu1101‘:‘zhangsan‘,‘stu1102‘:‘lizi‘}select-->zhangsanget-->lizi增加info["stu1101"]="add 1101"print(info)info["stu1104"]="xiaosan"#不存在则增加print(info)执行结果:{‘stu1103‘:‘wangwu‘,‘stu1101‘:‘add 1101‘,‘stu1102‘:‘lizi‘}{‘stu1103‘:‘wangwu‘,‘stu1104‘:‘xiaosan‘,‘stu1101‘:‘add 1101‘,‘stu1102‘:‘lizi‘}删除delinfo["stu1103"]print(info)执行结果:{‘stu1102‘:‘lizi‘,‘stu1101‘:‘zhangsan‘}多级字典嵌套#!/usr/bin/env pythonwww={"个人博客":{‘name‘:["lizhong"],‘address‘:["cloudstack.com"],},"运维社区":{‘name‘:["bjstack"],‘address‘:["bjstack.com"],},}www["个人博客"]["address"][0]+=",非常不错的个人博客,多多关注!"www["运维社区"]["address"][0]+=",社区活跃度非常高,期待你的加入!"print("猛击有惊喜-->",www["个人博客"]["address"])print("猛击有惊喜-->",www["运维社区"]["address"])执行结果:猛击有惊喜-->[‘cloudstack.com,非常不错的个人博客,多多关注!‘]猛击有惊喜-->[‘bjstack.com,社区活跃度非常高,期待你的加入!‘]

1.2 set集合

1.去重,把一个列表变成集合,就自动去重了
2.关系测试,测试两组数据之间的交集,差集,并集等关系。

创建一个数值集合

list_1=set{1,4,5,7,3,6,7,9}list_2=set{2,6,0,66,22,8,4}交集(list_1 & list_2)print(list_1.intersection(list_2))并集print(list_1.union(list_2))差集print(list_1.difference(list_2))print(list_2.difference(list_1))子集print(list_1.issubset(list_2))父集list_3=set([1,3,7])print(list_3.issubset(list_1))#list_3是list_1的子集print(list_1.issuperset(list_3))对称差集(list_1 ^ list_2)print(list_1.symmetric_difference(list_2))#对称差集,互相没有的取出来,去掉两个两个集合中重复的

反向差集

添加

list_1.add(999)#添加一项list_1.update([888,777,555])#添加多项print(list_1)删除print(list_1.discard(555))#discard不存在不会报错print(list_1.remove(888))#remove不存在,会报错,删掉了也不会返回数据print(list_1)

1.3 文件操作

文件句柄=file(‘文件路径‘,‘模式‘)注:Python中打开文件有两种方式,open(...)和file(...)本质上前者在内部会调用后者来进行文件操作,推荐使用open。python3中file模式已经取消1.打开文件打开文件模式有:f=open(‘db‘,‘r‘)# 只读模式,{默认}f=open(‘db‘,‘w‘)# 只写模式,{不可读;不存在则创建;存在则删除内容;}f=open(‘db‘,‘a‘)# 追加模式,{可读;不存在则创建;存在则只追加内容;}f=open(‘db‘,‘x‘)# 文件存在,则报错;不存在,则创建并写内容"+"表示可以同时读写某个文件:f=open(‘db‘,‘r+‘)# 可读写文件,{可读;可写;可追加}f=open(‘db‘,‘w+‘)# 写读。f=open(‘db‘,‘a+‘)# 同a。"U"表示在读取时,可以将\r\n自动转换成\n(与r或r+模式同使用)f=open(‘db‘,‘rU‘)f=open(‘db‘,‘r+U‘)"b"表示处理二进制文件(如ftp上传ISO镜像,Linux忽略,windows处理二进制文件时需标注)f=open(‘db‘,‘rb‘)# 二进制只读f=open(‘db‘,‘wb‘)# 二进制只写f=open(‘db‘,‘ab‘)# 二进制追加f=open(‘db‘,‘r‘)data=f.read()print(data,type(data))f.close()2.操作文件f=open(‘db‘,‘r+‘,encoding="utf-8")data=f.read(1)# 如果打开模式无b,则read,按照字符读取print(f.tell())# tell当前指针所在的位置(字节)f.seek(f.tell())# 调整当前指着你的位置(字节)f.write("777")# 当前指针位置开始覆盖print(data)# 打印输出f.close()# 关闭当前文件通过源码查看功能read()# 无参数,读全部;有参数,b字节,无b按字符tell()# 获取当前指针位置(字节)seek()# 指针跳转到指定位置(字节)write()# 写数据,b:字节,无b:字符close()# 关闭文件fileno()# 文件描述符flush()# 刷新文件内部缓冲区readline()# 仅读取一行truncate()# 截取,指针位置后的清空3.for循环文件对象f=open(“”)forlineinf:print(line)4.文件修改# db文件里面有"xuliangwei"字符串f=open("db","r",encoding="utf-8")f_new=open("db.bak","w",encoding="utf-8")forlineinf:if"xuliangwei"inline:line=line.replace("xuliangwei","xuliangwei.com")f_new.write(line)f.close()f_new.close()5.关闭文件f.close()#直接close文件避免打开文件后忘记关闭,统一通过管理上下文,即:withopen(‘db1‘,‘r‘)asf1,open("db2",‘w‘)asf2:pass

1.4Python字符编码转换

asiica 不支持中文
utf-8 一个汉子:三个字节
gbk 一个汉子:二个字节

Python3中默认字符编码是utf-8

#!/usr/bin/env pythonname=‘李众‘print(name.encode(‘UTF-8‘))# 转为UTF-8编码print(name.encode(‘GBK‘))# 转为GBK编码print(name.encode(‘ASCII‘))# 转为ASCII编码

Python2.X中默认字符编码是unicode
技术分享

python(三)

评论关闭