0005-20180422-自动化第六章-python基础学习笔记
day6
内容回顾:
1. 变量
2. 条件
3. while循环
4. 数据类型
- int
- bit_length
- bool
- true/false
- str
- upper
- lower
- strip
- split
- replace
- join
- capitalize
- startswith
- endswith
- isdecimal
- list
- append
- insert
- reverse
- tuple
-
- dict
- get
- items
- keys
- values
5. for
6. range
7. 编码
ascii
unicode
utf-8
gbk
8. else
if xxx:
pass
else:
pass
while true:
pass
else:
pass
for item in xxx:
pass
else:
# 当可迭代对象中的元素,被循环完毕后执行一次
pass
例题:
while true:
for k in dic:
if 'k' in k:
del dic[k] #删除后如果没有下一行的break跳出此轮的for循环则字典会报错----删除了循环中的字典键值
break
else: #for循环结束的标志----使用else
break
print(dic)
while true:
for k in dic:
if 'k' in k:
del dic[k] #删除后如果没有下一行的break跳出此轮的for循环则字典会报错----删除了循环中的字典键值
break
break
print(dic)
今日内容:
1. 数据类型
- bytes -----gbk,utf-8,..编码
表现形式:name = b"alex"
name = b'\xe6\xb5\xb7\xe5\xa8\x87' #海娇 #如果有中文表现形式为16进制字符
内部原理:utf-8或gbk...
- upper
***bytes 里的功能同str一样,只是表现形式不同而已***
- lower
- strip
- split
- replace
- join
- capitalize
- startswith
- endswith
- isdecimal
- int
- bit_length
- to_bytes ,将数字转换为字节类型,6表示用多少个字节表示,little/big用于指定生成字节的顺序
val = 66
#计算机硬件cpu处理的大端,小端
result=val.to_bytes(6,'big')#6控制的是生成多少个字节的字符,big表示大端,cpu处理时,指示是从后往前读(big)还是从前往后读(little)
print(result)
#b'\x00\x00\x00\x00\x00b'
val = 66
#计算机硬件cpu处理的大端,小端
result=val.to_bytes(6,'little')#6控制的是生成多少个字节的字符,big表示大端,cpu处理时,指示是从后往前读(big)还是从前往后读(little)
print(result)
#b'b\x00\x00\x00\x00\x00'#此处显示3位,大写的b是assic码表示66的编码,与二进制没有关系
- from_bytes #将字节再转换为数字
val = 2
result=val.to_bytes(6,'little')
print(result)
#将字节再转换为数字:
data = int.from_bytes(result,'little')
print(data)
#b'\x02\x00\x00\x00\x00\x00'
#2
- bool
- true/false
- str ----unicode编码 #py3中默认 name = "alex" --->网络传输或存硬盘则转换为字节:name = b"alex"
表现形式:name = "alex"
name = '海娇' # 海---占4个字节的01,娇---占4个字节的01
内部原理:unicode
示例:
# a = "alex" 通过gbk编码在硬盘或网络传输中的01查看
a = "alex".encode('gbk')
for i in a:
print(bin(i))
# d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py
# 0b1100001
# 0b1101100
# 0b1100101
# 0b1111000
name = "海娇".encode('gbk')
for i in name:
print(bin(i))
d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py
0b10111010
0b10100011
0b10111101
0b10111111
name_str = "海娇" #默认unicode编码格式8个字节
name_tytes = name_str.encode('utf-8') #转换为utf-8编码后6个字节
print(name_tytes)
# d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py
# b'\xe6\xb5\xb7\xe5\xa8\x87'
name_str = "海娇" #默认unicode编码格式8个字节
name_tytes = name_str.encode('gbk') #转换为gbk编码后4个字节
print(name_tytes)
# d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py
# b'\xba\xa3\xbd\xbf'
#二进制打印字符串以utf-8编码的字符------海娇----在内存中的二进制编码
name_str = "海娇" #默认unicode编码格式8个字节
name_tytes = name_str.encode('utf-8') #转换为gbk编码后4个字节
for i in name_tytes:
print(bin(i))
# b'\xe6\xb5\xb7\xe5\xa8\x87'
# d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py
# 0b11100110
# 0b10110101
# 0b10110111
# 0b11100101
# 0b10101000
# 0b10000111
name_str = "查显忠" #默认unicode编码格式12个字节
name_tytes = name_str.encode('utf-8') #转换为gbk编码后9个字节
print(name_tytes)
for i in name_tytes:
print(bin(i))
d:\install-file\python-work\venv\scripts\python.exe d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py
b'\xe6\x9f\xa5\xe6\x98\xbe\xe5\xbf\xa0'
0b11100110
0b10011111
0b10100101
0b11100110
0b10011000
0b10111110
0b11100101
0b10111111
0b10100000
- upper
- lower
- strip
- split
- replace
- join
- capitalize
- startswith
- endswith
- isdecimal
- encode('utf-8或gbk') #把字符串转换为字节
示例:
name = "海娇"
#把unicode编码的字符串,转换为utf-8编码的字节:
v1 = name.encode('utf-8')
#把unicode编码的字符串,转换为gbk编码的字节:
v2 = name.encode('gbk')
print(v1)
print(v2)
# b'\xe6\xb5\xb7\xe5\xa8\x87'
# b'\xba\xa3\xbd\xbf'
#以上utf-8和gbk编码的海娇互相不认识
- casefold #可以转换所有语种的大写转小写
- list
- append
- insert
- reverse
- tuple
-
- dict
- get
- items
- keys
- values
a、字节和字符串相关:
内存:unicode编码的0101
硬盘:utf-8 gbk gb2312编码的0101
本质上,内存中都是01010101,具体是什么样子的01,需要根据编码来决定。
不同编码的0101,互相不认识。
a = "alex"
python3:
utf-8 -> 010100101
特殊的:
字符串:
表现上:a = "alex"
# ()本质是unicode编码的010101
字节:bytes
表现:a = b"alex"
本质:010101,某种编码:utf-8,gbk,...
总结:
python3
str --> unicode
bytes --> gbk,utf-8,...
2. 文件操作
=============================脚本==========================
删除字典中带k字符的键值对脚本:
1 #删除字典中带k字符的键值对 2 """ 3 #方法1: 4 dic = { 5 'k1':'v1', 6 'y2':'v1', 7 'k11':'v1' 8 } 9 #把key重新创建一份 10 key_list = [] 11 for k in dic: 12 #print(k) 13 key_list.append(k) 14 #循环所有的key 15 for key in key_list: 16 #print(key) 17 if 'k' in key: 18 del dic[key] 19 print(dic) 20 """ 21 22 """ 23 #方法2 24 dic = { 25 'k1':'v1', 26 'y2':'v1', 27 'k11':'v1' 28 } 29 30 while true: 31 for k in dic: 32 if 'k' in k: 33 del dic[k] #删除后如果没有下一行的break跳出此轮的for循环则字典会报错----删除了循环中的字典键值 34 break 35 else: #for循环结束的标志----使用else 36 break 37 print(dic) 38 39 """ 40 41 42 """ 43 #方法3 44 45 dic = { 46 'k1':'v1', 47 'y2':'v1', 48 'k11':'v1' 49 } 50 51 for key in list(dic.keys()): 52 if 'k' in key: 53 del dic[key] 54 print(dic) 55 """
设置for循环完毕的条件脚本:
1 #设置for循环完毕的条件: 2 3 li = [11,22,33,44,55] 4 for item in li: 5 print(item) 6 else: 7 print('for-循环完毕')
数据类型补充知识相关脚本:
1 """ 2 # a = "alex" 通过gbk编码在硬盘或网络传输中的01查看 3 a = "alex".encode('gbk') 4 for i in a: 5 print(bin(i)) 6 7 # d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py 8 # 0b1100001 9 # 0b1101100 10 # 0b1100101 11 # 0b1111000 12 13 """ 14 15 """ 16 name = "海娇".encode('gbk') 17 for i in name: 18 print(bin(i)) 19 # d: / install - file / python - work / auto - chapter6 / 3 - data -class -supplement.py 20 # 0b10111010 21 # 0b10100011 22 # 0b10111101 23 # 0b10111111 24 25 """ 26 27 """ 28 name_str = "海娇" #默认unicode编码格式8个字节 29 name_tytes = name_str.encode('utf-8') #转换为utf-8编码后 30 31 print(name_tytes) 32 # d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py 33 # b'\xe6\xb5\xb7\xe5\xa8\x87' 34 """ 35 36 """ 37 name_str = "海娇" #默认unicode编码格式8个字节 38 name_tytes = name_str.encode('gbk') #转换为gbk编码后4个字节 39 40 print(name_tytes) 41 42 # d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py 43 # b'\xba\xa3\xbd\xbf' 44 """ 45 46 """ 47 name_str = "海娇" #默认unicode编码格式8个字节 48 name_tytes = name_str.encode('utf-8') #转换为gbk编码后4个字节 49 for i in name_tytes: 50 print(bin(i)) 51 52 # d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py 53 # 0b11100110 54 # 0b10110101 55 # 0b10110111 56 # 0b11100101 57 # 0b10101000 58 # 0b10000111 59 """ 60 61 """ 62 name_str = "查显忠" #默认unicode编码格式8个字节 63 name_tytes = name_str.encode('utf-8') #转换为gbk编码后4个字节 64 print(name_tytes) 65 for i in name_tytes: 66 print(bin(i)) 67 68 # d:\install-file\python-work\venv\scripts\python.exe d:/install-file/python-work/auto-chapter6/3-data-class-supplement.py 69 # b'\xe6\x9f\xa5\xe6\x98\xbe\xe5\xbf\xa0' 70 # 0b11100110 71 # 0b10011111 72 # 0b10100101 73 # 0b11100110 74 # 0b10011000 75 # 0b10111110 76 # 0b11100101 77 # 0b10111111 78 # 0b10100000 79 80 """ 81 82 """ 83 name = "海娇" 84 85 #把unicode编码的字符串,转换为utf-8编码的字节: 86 v1 = name.encode('utf-8') 87 #把unicode编码的字符串,转换为gbk编码的字节: 88 v2 = name.encode('gbk') 89 print(v1) 90 print(v2) 91 92 # b'\xe6\xb5\xb7\xe5\xa8\x87' 93 # b'\xba\xa3\xbd\xbf' 94 #以上utf-8和gbk编码的海娇互相不认识 95 """
数据类型整数补充相关知识脚本:
1 ##################int########### 2 """ 3 val = 9 4 result = val.bit_length() #二进制位所占的最大长度 5 print(result) 6 """ 7 8 """ 9 val = 2 10 result=val.to_bytes(3,'big') 11 print(result) 12 b'\x00\x00\x02' 13 """ 14 15 """ 16 val = 2 17 result=val.to_bytes(6,'big') 18 print(result) 19 #b'\x00\x00\x00\x00\x00\x02' 20 """ 21 22 """ 23 val = 66 24 #计算机硬件cpu处理的大端,小端 25 result=val.to_bytes(6,'big')#6控制的是生成多少个字节的字符,big表示大端,cpu处理时,指示是从后往前读(big)还是从前往后读(little) 26 print(result) 27 #b'\x00\x00\x00\x00\x00b'#此处显示3位,大写的b是assic码表示66的编码,与二进制没有关系 28 29 #idle测试输出如下: 30 # >>> bin(66) 31 # '0b1000010' 32 # >>> 33 34 """ 35 36 """ 37 val = 66 38 #计算机硬件cpu处理的大端,小端 39 result=val.to_bytes(6,'little')#6控制的是生成多少个字节的字符,big表示大端,cpu处理时,指示是从后往前读(big)还是从前往后读(little) 40 print(result) 41 #b'b\x00\x00\x00\x00\x00'#此处显示3位,大写的b是assic码表示66的编码,与二进制没有关系 42 """ 43 44 val = 2 45 result=val.to_bytes(6,'little') 46 print(result) 47 48 #将字节再转换为数字: 49 data = int.from_bytes(result,'little') 50 print(data)
数据类型字符串补充相关知识脚本:
1 ##########str########## 2 #str 3 """ 4 name = 'alex' 5 result = name.casefold()#β在法语里通通过casefold转为小写ss 6 #casefold可以转换所有语种的大写转小写 7 print(result) 8 result = name.lower()#英文字母的转小写 9 print(result) 10 11 name = 'β' 12 result = name.casefold() 13 print(result) 14 15 """ 16 #center---在中间的两侧分别插入指定字符,总宽度为指定宽度 17 """ 18 name = 'alex' 19 val = name.center(20,'*') 20 print(val) 21 #********alex******** 22 """ 23 """ 24 name = 'ale' 25 val = name.center(20,'*') 26 print(val) 27 #********ale********* 28 """ 29 30 #rjust 和 ljust 分别在右侧或左侧插入指定字符,总宽度为指定字符 31 """ 32 name = 'alex' 33 val = name.ljust(20,'*') 34 print(val) 35 #alex**************** 36 """ 37 38 """ 39 name = 'alex' 40 val = name.rjust(20,'*') 41 print(val) 42 #****************alex 43 """ 44 45 #count 46 name =''
上一篇: ClientDataSet应用