7编码的进阶,文件的操作
1 编码的进阶
# str # s1 = 'alex' # print(s1,type(s1)) # s2 = b'alex' # print(s2.upper()) # print(s2,type(s2)) # s1 = '中国' # print(s1, type(s1)) # # b1 = s1.encode('utf-8') # print(b1,type(b1)) # # str ----> utf-8 bytes # s1 = 'a太白' # b1 = s1.encode('utf-8') # print(b1) # b'a\xe5\xa4\xaa\xe7\x99\xbd' # # str ----> gbk bytes # s1 = 'a太白' # b1 = s1.encode('gbk') # print(b1) # b'a\xcc\xab\xb0\xd7' # # # # utf-8 bytes ----> str Unicode # b3 = b'a\xe5\xa4\xaa\xe7\x99\xbd' # s2 = b3.decode('utf-8') # print(s2) # 终极转换 # utf-8 bytes gbk bytes # b3 = b'a\xe5\xa4\xaa\xe7\x99\xbd' # # # 现将b3 转化成UNicode # s = b3.decode('utf-8') # # # 再将 s 编码成 gbk # b4 = s.encode('gbk') # print(b4) # b'a\xcc\xab\xb0\xd7' # 由于utf-8 与gbk 的英文部分都是映射的ascii所以. (了解) # # s1 = 'alex!12' # b1 = s1.encode('utf-8') # s2 = b1.decode('gbk') # print(s2)
2 文件操作的初识
f = open("D:\美女护士教师主妇联系方式.txt",encoding='utf-8',mode='r') print(f.read()) f.close() """ f 变量: f,f1,file,file_handler,fh,f_h..... 文件句柄. open() python的内置函数,python提供的一个功能, 底层调用的是操作系统的调用文件命令,接口. windows: 编码:gbk. linux,ms: 编码是utf-8. 操作文件的流程: 1,打开文件,产生一个文件句柄. 2, 对文件句柄进行相应的操作. 3,关闭文件句柄. """
3 文件的读
# r r+ rb r+b # r # read() 全部读取 # f1 = open('r模式',encoding='utf-8') # content = f1.read() # print(content,type(content)) # f1.close() # read(n) ''' r模式: n 字符 rb模式: n 字节 ''' # f1 = open('r模式',encoding='utf-8') # print(f1.read(3)) # f1.close() # f1 = open('r模式',mode='rb') # print(f1.read(3)) # f1.close() # readline() 按行读取 # f1 = open('r模式',encoding='utf-8') # print(f1.readline().strip()) # print(f1.readline().strip()) # # f1.close() # readlines() # 返回一个list 列表的每个元素是源文件的每一行. # f1 = open('r模式',encoding='utf-8') # print(f1.readlines()) # f1.close() # 循环读取 # f1 = open('r模式',encoding='utf-8') # for line in f1: # print(line) # f1.close() # rb 以字节的形式读取 # 带b的一般操作的都是非文字类的文件. # f1 = open('美女.jpg',mode='rb') # print(f1.read()) # f1.close() # f1 = open('r模式',mode='rb') # print(f1.read()) # f1.close() # r+ 读写模式:先读后写 # f1 = open('r模式',encoding='utf-8') # content = f1.read() # print(content,type(content)) # f1.close() # f1 = open('r模式',encoding='utf-8',mode='r+') # content = f1.read() # print(content) # f1.write('666') # f1.close() f1 = open('r模式',encoding='utf-8',mode='r+') f1.write('666') print(f1.read()) f1.close()
4 文件的写
# w w+ wb w+b # w # 没有文件,创建文件,写入内容 f = open('w模式',encoding='utf-8',mode='w') f.write('随便写一点') f.close() # 如果有文件,先清空内容,后写入 # f = open('w模式',encoding='utf-8',mode='w') # # f.write('1alex is a lower man\n') # # f.write('1alex is a lower man\n') # # f.write('1alex is a lower man\n') # # f.write('1alex is a lower man\n') # for i in range(4): # f.write('Alex is a lower man\n') # f.close()
5 文件的追加
# a ab a+ a+b # a 没有文件,创建文件,写入内容 f = open('a模式',encoding='utf-8',mode='a') f.write('很多让人很有成就感的事情') f.write('很多让人很有成就感的事情') f.write('很多让人很有成就感的事情') f.close() # f = open('a模式',encoding='utf-8',mode='a') # f.write('666') # f.close() # read() readline readlines write close
6 其他操作方法
f1 = open('r模式',encoding='utf-8') print(f1.read()) # print(f1.fileno()) # 用不到 f1.close() # f1 = open('其他操作方法',encoding='utf-8',mode='w') # f1.write('jfkdlsfjdsafkds') # f1.flush() # 强制保存 # f1.close() # readable writeable # f1 = open('其他操作方法',encoding='utf-8') # # print(f1.readable()) # True # # f1.write('fdsafs') # print(f1.writable()) # False # if f1.writable(): # f1.write('fhdsklafjds') # f1.close() # seek tell # 网络编程: FTP的作业,断点续传的功能. seek tell # f1 = open('其他操作方法',encoding='utf-8') # # f1.seek(9) # 按照字节调整光标位置 # print(f1.tell()) # 获取光标的位置 # print(f1.read()) # print(f1.tell()) # f1.close() # truncate # 对原文件进行截取 # 他必须在可写情况下使用. # f = open('其他操作方法',encoding='utf-8',mode='r+') # f.seek(3) # 调整光标对truncate不管用 # f.truncate(9) # truncate都是从文件的开始进行截取,以字节为单位. # f.close() # 总结: # 最常用的几个方法 read seek tell flush readable writeable # 打开文件的第二种方式: # 优点: # 1,省去了写f.close() # with open('其他操作方法',encoding='utf-8') as f1: # print(f1.read()) # 2,一个with语句可以操作多个文件句柄. # with open('其他操作方法',encoding='utf-8') as f1,\ # open('r模式',encoding='utf-8',mode='w') as f2: # print(f1.read()) # f2.write('5435435') # f2.write('dhfjkdshjfdskhf') # 缺点: # IOError with open('其他操作方法', encoding='utf-8') as f1: print(f1.read()) # ******各种逻辑****** f1.close() with open('其他操作方法', encoding='utf-8', mode='w') as f2: pass
7 文件的改
# 1,以读的模式打开原文件. # 2,以写的模式创建一个新文件. # import os # with open('alex自述',encoding='utf-8') as f1,\ # open('alex自述.bak',encoding='utf-8',mode='w') as f2: # # 3,将原文件内容读取出来,按照你的要求改成新内容,写入新文件. # old_content = f1.read() # new_content = old_content.replace('alex','sb') # f2.write(new_content) # # 4,删除原文件. # os.remove('alex自述') # # 5,将新文件重命名成原文件. # os.rename('alex自述.bak','alex自述') # 高大上版 # # 1,以读的模式打开原文件. # 2,以写的模式创建一个新文件. # import os # with open('alex自述',encoding='utf-8') as f1,\ # open('alex自述.bak',encoding='utf-8',mode='w') as f2: # # 3,将原文件内容读取出来,按照你的要求改成新内容,写入新文件. # for old_line in f1: # new_line = old_line.replace('alex','sb') # f2.write(new_line) # # # 4,删除原文件. # os.remove('alex自述') # # 5,将新文件重命名成原文件. # os.rename('alex自述.bak','alex自述') # ret = 'alex dfjaslkdf' # print(ret.replace('太白','男神'))
8 深浅copy
# 赋值运算 变量指向的是同一个. # l1 = [1, 2, 3, [11, 22]] # l2 = l1 # # l2.append(666) # l2[-1].append(666) # # print(l1) # # print(l2) # print(l1) # print(l2) # 浅copy 列表举例 # dict set # copy 复制, 浅浅的复制一份. l1 = [1, 2, 3, [11, 22]] l2 = l1.copy() # print(l2) # [1, 2, 3, [11, 22]] # print(id(l1)) # print(id(l2)) # print(id(l1[0])) # print(id(l2[0])) # print(id(l1[-1])) # print(id(l2[-1])) # 给l1添加一个元素,l2 不变 # l1.append(666) # l1[-1].append(666) # print(l1) # print(l2) # l1 = [1, 2, 3, [11, 22]] # l2 = l1.copy() # l1[0] = 5 # print(l1) # print(l2) # 浅copy: 无论是同一个代码块,不同代码块下: # 复制一个外壳(列表),但是列表里面的所有元素,都共用一个. # 深copy : 深深的复制一下. import copy l1 = [1, 2, 3, [11, 22]] l2 = copy.deepcopy(l1) # print(l1) # print(l2) # print(id(l1),id(l2)) # 不可变数据类型 # 共用一份 # print(id(l1[0])) # 1868196880 # print(id(l2[0])) # 1868196880 # 可变的数据类型 重新创建一份 # print(id(l1[-1])) # 2158525445320 # print(id(l2[-1])) # 2158525446792 # l1[-1].append(666) # print(l1) # print(l2) # 深copy: 论是同一个代码块,不同代码块下: # 不仅创建一个新外壳(列表),外壳里面的可变的数据类型也创建一份新的,但是不可变的数据类型共用一个. # 面试题 # l1 = [1, 2, [11,]] # l2 = l1[:] # 全切: 浅copy # l1[-1].append(666) # print(l2) # l1 = [1, 2, [11,]] # l2 = l1.copy() # l1[-1].append(666) # print(l2)