• 编码转换 文件的操作


    1.编码的转换

    1 s1 = "汤圆"
    2 s2 = s1.encode("utf-8")   将s1的Unicode编码转换成utf-8格式的编码
    3 s3 = bs.decode("utf-8")   将s2的utf-8格式的编码转换成Unicode的编码
    4 bs2 = s2.encode("gbk")    将bs的Unicode格式的编码转换成gbk格式的编码

    2.文件的操作流程

    操作文件的流程:
    1,打开文件,产生一个文件句柄.
    2, 对文件句柄进行相应的操作.
    3,关闭文件句柄.

    1 f = open("D:美女校花联系方式.txt",encoding='utf-8',mode='r')
    2 print(f.read())
    3 f.close()

    3.文件的常用操作:

    r:

    ①.read()全部读取

    1 f1 = open = ('r模式',encoding = 'utf-8')
    2 comment = f1.read()
    3 print(comment)
    4 f1.close()

    ②.read(n)

    在r模式下就是:  读取前n个字符

    在rb模式下就是:读取前n个字节

    1 f1 = open('r模式',encoding = 'utf-8',mode = 'r'2 print(f1.read(3))
    3 f1.close()
    4 
    5 f2 = open('r模式',encoding = 'utf-8',mode = 'rb')
    6 print(f2.read(3))
    7 f2.close()

    ③.readline():按行读取

    1 f1 = open('r模式',encoding='utf-8')
    2 print(f1.readline().strip())
    3 print(f1.readline().strip())
    4 f1.close()

    ④readlines():返回一个list 列表的每个元素是源文件的每一行.  一次性读取全部文件内容,容易导致内存崩盘,不推荐使用。

    1 f1 = open('r模式',encoding='utf-8')
    2 print(f1.readlines())
    3 f1.close()
    结果:['1 ', '22 ', '33 ', '44 ', '55']

    ⑤循环读取,一般读取文件均使用这种方法。

    1 f1 = open('r模式',encoding = 'utf-8'2 for line in f1:
    3     print(line.strip())
    4 f1.close()

    rb:以字节的方式读取,操作的一般都是非文字类型的文件

    1 f1 = open('美女.jpg',mode='rb')
    2 print(f1.read())
    3 f1.close()

    4 f1 = open('r模式',mode='rb')
    5 print(f1.read())
    6 f1.close()

    r+ 读写模式:先读后写

    f1 = open('r模式',encoding='utf-8',mode='r+')
    content = f1.read()
    print(content)
    f1.write('666')
    f1.close()
    正常的读取,并将666写在末尾
    f1
    = open('r模式',encoding='utf-8',mode='r+') f1.write('666') print(f1.read()) f1.read() f1.close() #从光标那里开始读取,所以读取不到666
    将666写在开头,然后读取666以后的内容

    文件的写:

    w

     1 没有文件,创建文件,写入内容
     2 f = open('w模式',encoding='utf-8',mode='w')
     3 f.write('随便写一点')
     4 f.close()
     5 如果有文件,先清空内容,后写入
     6 f = open('w模式',encoding='utf-8',mode='w')
     7 f.write('1alex is a lower man
    ')
     8 f.write('1alex is a lower man
    ')
     9 f.write('1alex is a lower man
    ')
    10 f.write('1alex is a lower man
    ')
    11 for i in range(4): 12 f.write('Alex is a lower man ') 13 f.close()

    wb

    1 f1 = open('毕业照.jpg',mode='rb')
    2 content = f1.read()
    3 f1.close()
    4 
    5 f2 = open('毕业照1.jpg',mode='wb')
    6 f2.write(content)
    7 f2.close()

    文件的追加

    a 没有文件,创建文件,写入内容;有文件就添加上  (该模式不可读)

    1 f = open('a模式',encoding='utf-8',mode='a')
    2 f.write('很多让人很有成就感的事情')
    3 f.write('很多让人很有成就感的事情')
    4 f.write('很多让人很有成就感的事情')
    5 f.close()

    文件的其他操作:

    判断是否可写,判断是否可读。

    writeable;readable

    1 f1 = open('其他操作方法',encoding='utf-8')
    2 # print(f1.readable())  # True
    3 # f1.write('fdsafs')
    4 print(f1.writable())  # False
    5 if f1.writable():
    6     f1.write('fhdsklafjds')
    7 f1.close()

    seek  和   tell

    1 f1 = open('其他操作方法',encoding='utf-8')
    2 f1.seek(9)  # 按照字节调整光标位置  #9字节
    3 print(f1.tell())  # 获取光标的位置
    4 print(f1.read())
    5 print(f1.tell())
    6 f1.close()

    结果:

    1 9
    2 教育非常好.
    3 太白金星是最好的讲师.
    4 python20学生非常棒!
    5 84
     1 打开文件的第二种方式:
     2 
     3 优点:
     4 1,省去了写f.close()
     5 with open('其他操作方法',encoding='utf-8') as f1:
     6     print(f1.read())
     7 
     8 2,一个with语句可以操作多个文件句柄.
     9 
    10 with open('其他操作方法',encoding='utf-8') as f1,
    11         open('r模式',encoding='utf-8',mode='w') as f2:
    12     print(f1.read())
    13     f2.write('5435435')
    14     f2.write('dhfjkdshjfdskhf')

    文件的改

     1 1,以读的模式打开原文件.
     2 2,以写的模式创建一个新文件.
     3 import os
     4 with open('alex自述',encoding='utf-8') as f1,
     5     open('alex自述.bak',encoding='utf-8',mode='w') as f2:
     6 # # 3,将原文件内容读取出来,按照你的要求改成新内容,写入新文件.
     7     old_content = f1.read()
     8     new_content = old_content.replace('alex','sb')
     9     f2.write(new_content)
    10 # # 4,删除原文件.
    11 os.remove('alex自述')
    12 # # 5,将新文件重命名成原文件.
    13 os.rename('alex自述.bak','alex自述')

    高大上版本:

     1 # 1,以读的模式打开原文件.
     2 # 2,以写的模式创建一个新文件.
     3 import os
     4 with open('alex自述',encoding='utf-8') as f1,
     5     open('alex自述.bak',encoding='utf-8',mode='w') as f2:
     6 # 3,将原文件内容读取出来,按照你的要求改成新内容,写入新文件.
     7     for old_line in f1:
     8         new_line = old_line.replace('alex','sb')
     9         f2.write(new_line)
    10 
    11 # 4,删除原文件.
    12 os.remove('alex自述')
    13 # 5,将新文件重命名成原文件.
    14 os.rename('alex自述.bak','alex自述')
  • 相关阅读:
    JavaScript之DOM
    MapReduce多种join实现实例分析(一)
    JavaScript的文档对象模型DOM
    JavaScript原生实现观察者模式
    Idea环境下git 图形化操作
    一分钟教你如何实现唯美的文字描边
    Redis集群
    JAVA线程池的实际运用
    Java 线程池(ThreadPoolExecutor)原理解析
    win7开始菜单路径
  • 原文地址:https://www.cnblogs.com/chitangyuanlai/p/10548153.html
Copyright © 2020-2023  润新知