zoukankan      html  css  js  c++  java
  • 预科班D12

    2020.09.22星期二  预科班D12

    学习内容:

    一、修改文件的两种方式

    1、方案一

      思路:(1)先以r形式打开源文件

            (2)将源文件内容一次性读入内存中,在内存中修改完毕

            (3)以w形式打开源文件(源文件中内容被清空)

            (4)将内存中的文件写入源文件中

    with open("a.txt", mode = 'r', encoding = 'utf-8') as f:
        data = f.read()
        res = data.replace('jack', 'JACK')
    with open("a.txt", mode = 'w', encoding = 'utf-8') as f1:
        f1.write(res)

    2、方案二

      思路:(1)先以r形式打开源文件,再以w形式打开新的文件

         (2)每从源文件中读取一行内容就立即修改,然后将修改的内容写入新文件中

         (3)删除源文件,将新文件重命名为源文件名

    import os
    
    with open("a.txt", mode = "r", encoding = "utf-8") as src_f, 
            with open(".a.txt.swp", mode = "w", encoding = "utf-8") as dst_f:
        for line in src_l:
            res = line.replace("ccc", "CCC")
            dst_f.write(res)
    os.remove("a.txt")
    os.rename(".a.txt.swp", "a.txt")

    二、文件处理

    1、a模式打开文件直接在文件末尾输入

    with open(r"H:PycharmProjects	esta.txt", mode = 'a', encoding = 'utf-8') as f:
        f.write("111
    ")
        f.write("222
    ")
        f.write("333
    ")
    
    with open(r"H:PycharmProjects	esta.txt", mode = 'a', encoding = 'ytf-8') as f:
        f.write("444
    ")

    2、w模式打开文件后先将源文件内容清空再输入(命令未结束前也是一直往后写)

    with open(r"H:PycharmProjects	esta.txt", mode = 'w', encoding = "utf-8") as f:
        f.write("111
    ")
        f.write("222
    ")
        f.write("333
    ")
    with open(r
    "H:PycharmProjects esta.txt", mode = 'w', encoding = "utf-8") as f: f.write("444 ")

    三、字符编码

    字符==========编码==========》数字

    字符《========解码===========数字

    字符编码表:字符与数字的对应关系

    ASCⅡ表:英文字符与数字的关系

          用1Bytes对应一个英文字符

    GBK表:英文字符与中文字符跟数字的对应关系

          用2Bytes对应一个字符

    Shift-JIS:英文字符与日文字符跟数字的对应关系

    Euc-KR:英文字符与韩文字符跟数字的对应关系

    utf-8:"万国字符与数字的关系"

        1Bytes=====》1个英文字符

        3Bytes=====》1个中文字符

    四、处理文本文件

    with open('d.txt',mode='wt',encoding='utf-8') as f:
        f.write("哈哈哈哈")
    
    with open('d.txt', mode='rt', encoding='utf-8') as f:
        print(f.read())

    五、处理非文本文件

    src_file_path = input("请输入源文件路径:").strip()
    dst_file_path = input("请输入目标文件路径:").strip()
    src_file_path = r"%s" %src_file_path
    dst_file_patn = r"%s" %dst_file_path
    with open(src_file_path, mode = 'rb') as src_f, 
            open(dst_file_path, mode = 'wb') as dst_f:
        for line in scr_f:
            dst_f.write(line)

    data = src_f.read()

    dst_f.write(data)

    六、模块

    import os

    import time

    ...

    print('hello')

    time.sleep(3)

    print('world')

  • 相关阅读:
    ES各种错误解决
    ES 父子文档查询
    logstash jdbc 各种数据库配置
    ruby 疑难点之—— attr_accessor attr_reader attr_writer
    ruby 疑难点之—— yield 和 yield self
    aggregation 详解2(metrics aggregations)
    logstash multiline 把文件处理为单个 event
    aggregation 详解4(pipeline aggregations)
    aggregation 详解3(bucket aggregation)
    C++内存字节对齐规则
  • 原文地址:https://www.cnblogs.com/caojiaxin/p/13714625.html
Copyright © 2011-2022 走看看