zoukankan      html  css  js  c++  java
  • day03 文件操作

    目录

        1.文件操作实例

        2.文件常用操作

        3.with模块操作文件

    常用实例,把文件里面的内容读出来做成字典的形式在做成字列表展示。

    1.精简版。

    lst = []
    f = open("file01", mode="r", encoding="utf-8")
    for line in f:
    line1 = line.strip().split(",")
    dic = {}
    dic["id"] = line1[0]
    dic["name"] = line1[1]
    dic["price"] = line1[2]
    lst.append(dic)
    print(lst)

    2.升级版。

    f = open("file01", mode="r", encoding="utf-8")
    title_str = f.readline().strip()
    title_list = title_str.split(",")
    print(title_list)
    lst = []
    for body in f:
    body_str = body.strip().split(",")
    print(body_str)
    dic = {}
    for i in range(len(title_list)):
    dic[title_list[i]] = body_str[i]
    lst.append(dic)
    print(lst)

    一. 文件读写追加以及修改

    r:读  r+: 读写  rb: 以字节的形式读。一般用来操作图片视屏音乐等非文字信息。

    1.读
    f = open("file01", mode="r",encoding="utf-8")
    content = f.read()
    print(content)
    close() ps:读文件but这种方式是读所有 如果文件太大会内存溢出
    f = open("file01", mode="r",encoding="utf-8")
    for line in f:
    print(line) ps:一行一行读取
    f = open("file01", mode="r",encoding="utf-8")
    for line in f:
    input("按回车往下浏览:")
    print(line) ps:通过函数input()阻塞 让用户按下回车键继续往下浏览

    2.读写
    待补充....

    3.rb
    f = open("file01", mode="rb")
    content = f.read()
    print(content)

    ps:只要有四个方法
    read() 读
    readline()读一行
    readlines() 全部读并且以列表的形式呈现
    readable() 判断是否有读的权限

    w:写 w+:写读 wb:同上

    注意:如果没有⽂文件. 则会创建⽂文件, 如果⽂文件存在. 则将原件中原来的内容删除, 再 写入新内容 。

    1.w写
    f = open("file02.txt", mode="w", encoding="utf-8")
    f.write("Today is monday")
    f.flush()
    f.close() ps:写的时候最好用flush刷新。

    2.wb模式下可以不指定打开⽂文件的编码. 但是在写⽂文件的时候必须将字符串串转化成utf-8的 bytes数据 。
    f = open("file02.txt", mode="wb")
    f.write("金毛狮王".encode("utf-8"))
    f.flush()
    f.close()

    a:在文件的末尾追加内容 a+ 追加  ab:同上,不管在什么位置都是追加

    f = open("file02.txt", mode="a",encoding="utf-8")
    f.write("西游记")
    f.flush()
    f.close()

    修改:修改文件实际上是把原来的删除了改名覆盖原来的那一份。

    文件复制
    f = open("file02.txt", mode="r",encoding="utf-8")
    f2 = open("file03.txt",mode="w", encoding="utf-8")
    for i in f:
    f2.write(i)

    文件修改
    f = open("file02.txt", mode="r",encoding="utf-8")
    f1 = open("file022.txt", mode="w",encoding="utf-8")
    for i in f:
    i = i.replace("西游记","西游记后传")
    f1.write(i)

    f.close()
    f1.flush()
    f1.close()
    import os
    os.remove("file02.txt")
    os.rename("file022.txt","file02.txt")

    文件操作常见的函数
    seek()移动光标的位置 0:开头 1:当前 2.结尾 ps:移动是以bytes为单位,如果是utf8字符集需要乘以三
    tell()告诉当前光标的位置 中文算3个bytes 标点数字算一个
    f = open("file02.txt", mode="r+",encoding="utf-8")
    content = f.read()
    print(f.tell())
    f.seek(9)
    print(f.tell())

    truncate截断
    f = open("file02.txt", mode="r+",encoding="utf-8")
    content = f.seek(10)
    print(f.truncate())
    print(content)

    二. with模块实现文件的操作

    import os
    with open("file03.txt", mode="r",encoding="utf-8") as f1,
    open("file033.txt",mode="w", encoding="utf-8") as f2:
    for line in f1:
    f2.write(line)
    ps: with的操作不用写close() 会自己帮你检查。
    We are down, but not beaten. tested but not defeated.
  • 相关阅读:
    structInMemory
    合并字符串
    eggs
    1005. Spell It Right (20) -PAT
    60 人工智能
    50 计算机网络
    20数据结构
    40操作系统
    10 C/C++/python
    30汇编
  • 原文地址:https://www.cnblogs.com/guniang/p/10710378.html
Copyright © 2011-2022 走看看