python ------- 檔案處理之增刪改查-------作業

來源:互聯網
上載者:User

標籤:增刪改   開關   int   enter   logs   server   str   ack   lease   

有點小bug,還待改進,代碼如下:

 

1.查詢:

輸入:www.ordboy1.org

 1 list = []  #建立一個空列表 2 flag = False #設定一個開關 3 input1 = input("plesae your input "www.oldboy1.org" :") 4 with open("haproxy.conf",encoding="utf-8") as f_read:  #把源檔案以utf-8格式開啟到f_read 5     for line in f_read:  #遍曆檔案到line這個變數中 6         if line.startswith("backend") and input1 in line:  7             flag = True 8             continue  #跳出本層迴圈 9 10         if line.startswith("backend"): #判斷字串是否以backend開頭11             break12         if flag:13             list.append(line.strip()) #追加到空列表14 print("hello".center(50,"-"))15 16 for i in list:  #遍曆列表17     print(i)

 

 


2.刪除:

輸入:{"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}

 1 choice = input("please your input  is {} :") 2     # {"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,maxconn:3333}} 3     dic = eval(choice)  # 把輸入的字串轉成字典 4     tltle = dic["backend"] 5     tetle1 = dic["record"]  #這個沒有用 6     with open("haproxy.conf", encoding="utf-8") as f_read, open("test5", "w", encoding="utf-8") as w_write: 7  8         for line in f_read: 9             w_write.write(line)10             if line.startswith("backend") and tltle in line:11                 # w_write.write(line)12                 break

 

3.增加:

輸入:{"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}

 1 choice = input("please your input is {}  : ") 2     # {"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}} 3     dic = eval(choice)  # 把字串轉成字典 4     title = dic[‘backend‘]  # "www.oldboy20.org" 5     record = dic[‘record‘] 6     r_s = ‘        server %s weight %s maxconn %s\n‘  7           % (record[‘server‘], record[‘weight‘], record[‘maxconn‘]) 8  9     with open("haproxy.conf", encoding="utf-8") as f_read, open("test3", mode="w", encoding="utf-8") as w_read:10         for line in f_read:11             w_read.write(line)12             if line.startswith(‘backend‘) and title in line:13                 # print(‘11111>‘)14                 w_read.write(r_s)15                 # w_read.write(‘=====>‘)16         w_read.flush()

 

4.修改:

輸入:{"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}

 1 choice = input("please your input is {} :") 2     # {"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}} 3     dic = eval(choice)  #把輸入的字串轉成字典 4     tltel = dic["backend"] 5     tltel1 = dic["record"] 6     dic["record"]["server"] = "0000" 7     dic["record"]["weight"] = "0000" 8     dic["record"]["maxconn"] = "0000" 9     # print(dic)10     #格式化字串11     r_s = "        server %s weight %s maxconn %s" % (tltel1["server"], tltel1[‘weight‘], tltel1["maxconn"])12     with open("haproxy.conf", encoding="utf-8") as f_read, open("test6", mode="w", encoding="utf-8") as w_write:13         for line in f_read:14             w_write.write(line) # 把line中的字串寫到w_write中15             if line.startswith("backend") and tltel in line: #判斷 line中字串否以“backend”開頭,並且滿不滿足tetle在這行16                 w_write.write(r_s)  #把字串寫入到w_rite中17                 break   #跳出迴圈

 

python ------- 檔案處理之增刪改查-------作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.