Python基础三
目录
一、字典
二、字符串
三、集合
四、文件操作
五、字符编码
一、字典
字典是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:
d = {key1 : value1, key2 : value2 }
键必须是唯一的,但值则不必。
值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。
增加:
dic1 ={'name':'Cathy','stu': 101,'leve': 'low'}
dic1['salary']=5000 #增加一项
print(dic1)
执行结果:
{'name': 'Cathy', 'stu': 101, 'leve': 'low', 'salary': 5000}
修改:
dic1 ={'name':'Cathy','stu': 101,'leve': 'low'}
dic1['salary']=5000 #增加一项
print(dic1)
dic1['salary']=6000
print(dic1)
执行结果:
{'name': 'Cathy', 'stu': 101, 'leve': 'low', 'salary': 5000}
{'name': 'Cathy', 'stu': 101, 'leve': 'low', 'salary': 6000}
将salary由5000变成6000
删除:
dic1 ={'name':'Cathy','stu': 101,'leve': 'low','salary':6000,'sex':'lady'}
del dic1['name']#删除name
print(dic1)
dic1.pop('stu')#删除stu
print(dic1)
dic1.popitem()
print(dic1)#随机删除
执行结果:
{'stu': 101, 'leve': 'low', 'salary': 6000, 'sex': 'lady'}
{'leve': 'low', 'salary': 6000, 'sex': 'lady'}
{'leve': 'low', 'salary': 6000}
查找:
dic1 ={'name':'Cathy','stu': 101,'leve': 'low','salary':6000,'sex':'lady'}
print(dic1['name'])#取name的值
print(dic1.get('stu'))#取stu的值
print('stu'in dic1)#判断stu是否在dic1字典里面,存在返回ture
print('ggg' in dic1)#判断ggg是否在dic1字典里面,不存在返回false
执行结果:
Cathy
101
True
False
多级字典嵌套及操作:
dic1 ={ 'ET16':{ 'windows': ['win7','win8','win10'], 'mac':[10.11,10.12,10.13] }, 'ET18':{ 'h5':['chrome','firfox','opera'], 'sdk':['C++','Java','C#'] } } for i in dic1['ET18']: print(i) for i in dic1['ET18']['h5']: print(i)
执行结果:
h5
sdk
chrome
firfox
opera
一些其他用法:
dic1 ={ 'ET16':{ 'windows': ['win7','win8','win10'], 'mac':[10.11,10.12,10.13] }, 'ET18':{ 'h5':['chrome','firfox','opera'], 'sdk':['C++','Java','C#'] } } print(dic1.values())#取出值 print(dic1.keys())#取出key
执行结果:
dict_values([{'windows': ['win7', 'win8', 'win10'], 'mac': [10.11, 10.12, 10.13]}, {'h5': ['chrome', 'firfox', 'opera'], 'sdk': ['C++', 'Java', 'C#']}])
dict_keys(['ET16', 'ET18'])
dic1 ={ 'ET16':{ 'windows': ['win7','win8','win10'], 'mac':[10.11,10.12,10.13] }, 'ET18':{ 'h5':['chrome','firfox','opera'], 'sdk':['C++','Java','C#'] } } dic1.setdefault('M3000','new')#添加一个新的字典key和对应值 print(dic1)
执行结果:
{'ET16': {'windows': ['win7', 'win8', 'win10'], 'mac': [10.11, 10.12, 10.13]}, 'ET18': {'h5': ['chrome', 'firfox', 'opera'], 'sdk': ['C++', 'Java', 'C#']}, 'M3000': 'new'}
添加一个字典里没有的key,使用setdefault,直接就添加进去了,如果新加的key值是字典里存在的,就直接返回原来的值,看下面例子
dic1 ={ 'ET16':{ 'windows': ['win7','win8','win10'], 'mac':[10.11,10.12,10.13] }, 'ET18':{ 'h5':['chrome','firfox','opera'], 'sdk':['C++','Java','C#'] } } dic1.setdefault('ET16','old') print(dic1)
执行结果:
{'ET16': {'windows': ['win7', 'win8', 'win10'], 'mac': [10.11, 10.12, 10.13]}, 'ET18': {'h5': ['chrome', 'firfox', 'opera'], 'sdk': ['C++', 'Java', 'C#']}}
ET16:old并没有添加到字典中去。
dic1 ={'name': 'Cathy', 'stu': 101, 'leve': 'low', 'salary': 6000}
dic2 = {'name':'Claire',1:0,0:9}
dic1.update(dic2)#有原来的有的key对应的value值更新,没有的直接添加到里面
print(dic1)
print(dic1.items())#将字典变成列表
执行结果:
{'name': 'Claire', 'stu': 101, 'leve': 'low', 'salary': 6000, 1: 0, 0: 9}
dict_items([('name', 'Claire'), ('stu', 101), ('leve', 'low'), ('salary', 6000), (1, 0), (0, 9)])
dic3 = dict.fromkeys([1,2,3],[4,{'name':'cathy'},5])#初始化一个字典
dic3[2][1]['name']= 'claire'#所有的key对应的value值使用的是相同的地址
print(dic3)
执行结果:
{1: [4, {'name': 'claire'}, 5], 2: [4, {'name': 'claire'}, 5], 3: [4, {'name': 'claire'}, 5]}
本来只改了2的name,结果将所有的name都改了
循环字典:
dic1 = {'name': 'Cathy', 'stu': 101, 'leve': 'low', 'salary': 6000}
for i in dic1:
print(i,dic1[i])
print("--------分栏符-------")
for k,v in dic1.items():#会先把dict转成list,数据里大时莫用,执行效率就低了
print(k,v)
执行结果:
name Cathy
stu 101
leve low
salary 6000
--------分栏符-------
name Cathy
stu 101
leve low
salary 6000
二、字符串
字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。
特性:不可修改
name = 'my \tname is cathy' print(name.capitalize())#首字母大写 print(name.count('a'))#统计有多少个a print(name.center(50,'-')) print(name.encode())#将字符串编码成bytes格式 print(name.endswith('thy'))#判断字符串是否以thy结尾 print(name.expandtabs(tabsize=10))#输出'my name is cathy'将\t转换成多长的空格 print(name.find('is'))#查找is,找到返回其索引, 找不到返回-1 print(name[name.find('is'):14])#字符串也可以切片
执行结果:
My name is cathy
2
----------------my name is cathy-----------------
b'my \tname is cathy'
True
my name is cathy
9
is ca
name = 'my name is {name},i am {year}' print(name.format(name='cathy',year = 18))#格式化输出 print('32dfgfd'.isalnum())#是否包含数字和字母 print('hdQQQskjf'.isalpha())#是不是只有字母 print('222'.isidentifier())#判断是不是一个合法的标识符也就是变量名 print('3A'.isdigit())#判断是不是数字 print(','.join(['1','2','3','4']))#列表中间加个间隔符 print(name.ljust(50,'*'))#长度不够在右边用*补全
执行结果:
my name is cathy,i am 18
True
True
False
False
1,2,3,4
my name is {name},i am {year}*********************
s = str.maketrans('abcdef','123456') print('cathy ee'.translate(s))
执行结果:
31thy 55
name = 'My Name is cathy,i am 18' print(name.replace('i','I',1)) print(name.rfind('i'))#从左往右数,找到最后边的下标 print(name.split('m'))#按m变成列表 print(name.splitlines())#按换行变成列表 print(name.swapcase()) print(name.title())
执行结果:
My Name Is cathy,i am 18
17
['My Na', 'e is cathy,i a', ' 18']
['My Name is cathy,i am 18']
mY nAME IS CATHY,I AM 18
My Name Is Cathy,I Am 18
三、集合
集合是一个无序的,不重复的数据组合,它的主要作用如下:
- 去重,把一个列表变成集合,就自动去重了
- 关系测试,测试两组数据之前的交集、差集、并集等关系
常用操作
set_1 = set([1,2,3,4,5,6]) set_2 = set([7,8,9,4,5,6]) #交集 print(set_1.intersection(set_2)) print(set_1 & set_2) #并集 print(set_1.union(set_2)) print(set_1 | set_2) #差集 print(set_1.difference(set_2)) print(set_1-set_2) #子集 print(set_1.issubset(set([1,2,3,4,5,6,7,8]))) #父集 print(set_1.issuperset(set([1,2,3]))) #对称差集 print(set_1.symmetric_difference(set_2)) print(set_1 ^ set_2) #如果没有交集就返回True print(set([1,2,3]).isdisjoint(set([4,5,6]))) set_1.add(8)# 添加一项 print(set_1) set_1.update(['uuuuu', 'kkkkk', 'mmmmm']) # 在s中添加多项 print(set_1) set_1.remove(8)#删除指定一个 print(set_1) print(len(set_1))#长度 print(1 in set_1) #测试 1 是否是 set_1 的成员 print(10 not in set_1) #测试 10是否是 set_1 的成员 #print(set_1.pop())#随机删掉一个并且将值返回 set_1.discard(6)#元素存在就直接删除,不存在也不会报错 print(set_1)
执行结果:
{4, 5, 6}
{4, 5, 6}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 2, 3}
{1, 2, 3}
True
True
{1, 2, 3, 7, 8, 9}
{1, 2, 3, 7, 8, 9}
True
{1, 2, 3, 4, 5, 6, 8}
{1, 2, 3, 4, 5, 6, 'uuuuu', 8, 'kkkkk', 'mmmmm'}
{1, 2, 3, 4, 5, 6, 'uuuuu', 'kkkkk', 'mmmmm'}
9
True
True
{1, 2, 3, 4, 5, 'uuuuu', 'kkkkk', 'mmmmm'}
四、文件操作
对文件操作流程
- 打开文件,得到文件句柄并赋值给一个变量
- 通过句柄对文件进行操作
- 关闭文件
打开文件的模式有:
- r,只读模式(默认)。
- w,只写模式。【不可读;不存在则创建;存在则删除内容;】
- a,追加模式。【可读; 不存在则创建;存在则只追加内容;】
"+" 表示可以同时读写某个文件
- r+,可读写文件。【可读;可写;可追加】
- w+,写读
- a+,同a
"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)
- rU
- r+U
"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)
- rb
- wb
- ab
a,追加模式
f = open('shoppinglist2','a',encoding= 'utf-8')#文件句柄 f.write('hello czur\n') f.write('welcome\n')#在原来的文件上追加,不会覆盖之前的内容
w,只写模式。【不可读;不存在则创建;存在则删除内容;】
f = open('shoppinglist2','w',encoding= 'utf-8')#文件句柄 data = f.read() print(data)
执行结果:
Traceback (most recent call last):
File "C:/Users/Test/PycharmProjects/untitled1/filetest.py", line 3, in <module>
data = f.read()
io.UnsupportedOperation: not readable
r,只读模式(默认)
f = open('shoppinglist2',encoding= 'utf-8')#文件句柄 data = f.read() print(data)
执行结果:
输出shoppinglist的文件内容
高效的读取文件
f = open('shoppinglist','r',encoding= 'utf-8')#文件句柄 #一行一行读,效率高,读到内存中,读一行在内存中删一行 count = 0 for line in f: if count == 9: print('-------版本号不打印-----')#不打印第10行 count +=1 continue print(line.strip()) count +=1
文件tell和seek
tell当前光标位置,seek光标跳到指定位置
f = open('shoppinglist','r',encoding= 'utf-8')#文件句柄 print(f.tell())#查看光标当前位置 print(f.readline()) print(f.tell())#查看光标当前位置 print(f.readline()) f.seek(1)#将光标移到1这个位置 print(f.readline()) print(f.encoding)#当前文件的编码格式
打印一个进度条(flush的作用)
import sys,time for i in range(50): sys.stdout.write("#")#在屏幕上输出 sys.stdout.flush()#刷新 time.sleep(0.1)#等待0.1s,执行下一个循环
截断
f = open('shoppinglist','a',encoding= 'utf-8')#文件句柄 f.truncate(10)
截取前10个
r'+读写模式
f = open('shoppinglist','r+',encoding= 'utf-8')#文件句柄 读写 print(f.readline()) print(f.readline()) print(f.readline()) f.write('**********cathy**********') f.close()
执行结果:
读取前3行,在文件末尾写入**********cathy**********
写读w+
f = open('shoppinglist','w+',encoding= 'utf-8')#文件句柄 写读 f.write('**********cathy**********\n') f.write('**********cathy**********\n') f.write('**********cathy**********\n') print(f.tell()) print(f.seek(10)) print(f.readline()) f.close()
先写后读取,写完之后光标停留在最后面
执行结果:
81
10
cathy**********
追加读写a+
f = open('shoppinglist','a+',encoding= 'utf-8')#文件句柄 追加读写 f.write('---------cathy*-----------\n') f.write('---------cathy*-----------\n') f.write('---------cathy*-----------\n') print(f.tell()) print(f.seek(10)) print(f.readline()) f.close()
执行结果:
249
10
cathy**********
修改
f = open('shoppinglist','r',encoding= 'utf-8')#文件句柄 f_new = open('shoppinglist2','w',encoding= 'utf-8')#文件句柄 for line in f: if '大连*************'in line: line = line.replace('大连*************','大连') f_new.write(line) f.close() f_new.close()
将shoppinglist“大连*************”里面的这句话改成”大连“,并且将整个文件重新存到shoppinglist2中
with的用法
with open('shoppinglist','r',encoding= 'utf-8') as f ,\ open('shoppinglist2','r',encoding= 'utf-8') as f2:#同时打开两个文件,操作结束后,会自动关闭文件 for line in f: print(line) for line1 in f2: print(line1) #for 循环结束后,会自动关闭打开的文件
五、字符编码
Python3则把系统默认编码设置为了 UTF-8。之后,文本字符和二进制数据分别用str和bytes表示。str能表示Unicode 字符集中所有字符,而二进制字节数据则用全新的数据类型bytes表示。
import sys print(sys.getdefaultencoding())#打印默认的编码 s = '你好' s_utf8 = s.encode('utf-8')#将字符转换成utf-8,并且会以二进制打印 print(s_utf8) s_unicode_gbk = s_utf8.decode('utf-8').encode('gbk')#将utf8转换成unicode再转换成gbk,最后依旧是二进制打印 print(s_unicode_gbk) print(s_unicode_gbk.decode('gbk'))#如果想字符打印出,就并且在进行一次decode
执行结果:
utf-8
b'\xe4\xbd\xa0\xe5\xa5\xbd'
b'\xc4\xe3\xba\xc3'
你好
encode 与 decode
#str 与 bytes 之间的转换可以用 encode 和从decode 方法。 #encode : 字符str 到 字节bytes 的编码转换,默认用UTF-8编码; s = 'python自动化' print(s.encode()) print(s.encode('gbk')) #decode : 字节bytes 到 字符str的转换,通用使用 UTF-8 编码格式进行转换 print(b'python\xe8\x87\xaa\xe5\x8a\xa8\xe5\x8c\x96'.decode())#'Python大神' print(b'python\xd7\xd4\xb6\xaf\xbb\xaf'.decode('gbk'))#'Python大神'
执行结果:
b'python\xe8\x87\xaa\xe5\x8a\xa8\xe5\x8c\x96'
b'python\xd7\xd4\xb6\xaf\xbb\xaf'
python自动化
python自动化

浙公网安备 33010602011771号