zoukankan      html  css  js  c++  java
  • python(12)- 文件处理应用Ⅰ

    一、读取文件,打印第三行时后面加入“徐亚平”

     程序如下:

    count=0
    with open("test",mode="r",encoding="utf8") as f:
        for line in f:
     
            if count==2:
                line="".join([line.strip(),"徐亚平"])
    
            print(line.strip())
            count+=1
    

      

     1 Somehow, it seems the love I knew was always the most destructive kind
     2 不知为何,我经历的爱情总是最具毁灭性的的那种
     3 Yesterday when I was young
     4 昨日当我年少轻狂
     5 The taste of life was sweet
     6 生命的滋味是甜的
     7 As rain upon my tongue
     8 就如舌尖上的雨露
     9 I teased at life as if it were a foolish game
    10 我戏弄生命 视其为愚蠢的游戏
    test

    二、修改文件,第三行时后面加入“徐亚平”

    因为文件保存在相对应的内存块,故不能在文件前面和文件中间修改,只能在文件尾端追加。

    故此时在第三行尾加入“徐亚平”,只能新建文件,删除原先的保留新建的满足题目要求。

    程序如下:

    count=0
    with open("test2",encoding="utf8") as f_read,open("test3",mode="w",encoding="utf8") as f_write:
        for line in f_read:
            if count==2:
                 line="".join([line.strip(),"徐亚平
    "])
            f_write.write(line)
            count+=1
    import os
    os.rename("test2","test2_bak")
    os.rename("test3","test2")
    

      

    原文件test2

     1 Somehow, it seems the love I knew was always the most destructive kind
     2 不知为何,我经历的爱情总是最具毁灭性的的那种
     3 Yesterday when I was young
     4 昨日当我年少轻狂
     5 The taste of life was sweet
     6 生命的滋味是甜的
     7 As rain upon my tongue
     8 就如舌尖上的雨露
     9 I teased at life as if it were a foolish game
    10 我戏弄生命 视其为愚蠢的游戏
    test2

    三、打印并显示进度条

    import sys
    for i in range(100):
        s="
    %s%% %s"%(i+1,"#"*(i+1))
        sys.stdout.write(s)
        sys.stdout.flush()
        import time
        time.sleep(0.5)
    print(s)
    

      

    四、修改haproxy配置文件

    1、查
        输入:www.oldboy.org
        获取当前backend下的所有记录
    while True:
        m = input("please input url:").strip()
        flag = False
        l = []
        with open("haproxy.conf",encoding="utf8") as f_read:          #打开文件并赋值为f_read
            for line in f_read:                                          #逐行读取文件haproxy.conf
                if line.startswith("backend") and m in line:           #如果是“backend”开头并且“m”在此行中
                    flag=True                               #不打印不需要,改变标志位值,中止本次操作,重新开始for循环
                    continue
                 if line.startswith("backend") and flag:    #第二次读到“backend”开头所在的行,结束本层for全部循环
                    break
                if flag:                                     #读到“backend”开头并且“m”后面的行,追加此行到l列表中
                    l.append(line.strip())
        for i in l:                                         #逐条打印l列表中的数据
            print(i)
    

      

     1 global
     2         log 127.0.0.1 local2
     3         daemon
     4         maxconn 256
     5         log 127.0.0.1 local2 info
     6 
     7 defaults
     8         log global
     9         mode http
    10         timeout connect 5000ms
    11         timeout client 50000ms
    12         timeout server 50000ms
    13         option  dontlognull
    14 
    15 listen  stats :8888
    16         stats enable
    17         stats uri       /admin
    18         stats auth      admin:1234
    19 
    20 frontend oldboy.org
    21          bind 0.0.0.0:80
    22          option httplog
    23          option httpclose
    24          option  forwardfor
    25          log global
    26          acl www hdr_reg(host) -i www.oldboy.org
    27          use_backend www.oldboy.org if www
    28 
    29 backend www.oldboy1.org
    30         server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33
    31         server 10.10.10.1 10.10.10.1 weight 22   maxconn 2000
    32         server 2.2.2.4 2.2.2.4       weight 20   maxconn 3000
    33 
    34 backend www.oldboy2.org
    35         server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
    36 
    37 backend www.oldboy20.org
    38         server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33
    39 
    40 
    41 data=input():    {"backend":"www.oldboy1.org","record":{server:...,weight:...,maxconn:...}}
    haproxy配置文件
  • 相关阅读:
    数据移除的循环
    C#窗体多语言切换(简繁)
    C# 文字转换最简单的方法
    使用CodeDOM动态编译一个字符串表达式
    C#设置WebBrowser默认浏览器
    Image和字节数组互转
    苹果手机的SB系列(8)为什么没有短信全部删除?
    循环删除外键约束
    腾讯云COS自定义域名,支持https,CDN,私有访问
    腾讯云COS请求签名C#版
  • 原文地址:https://www.cnblogs.com/xuyaping/p/6669497.html
Copyright © 2011-2022 走看看