Python-------file processing-------job search and deletion

Source: Internet
Author: User
Tags haproxy

A bit small bug, also to be improved, the code is as follows:

1. Enquiry:

Input: www.ordboy1.org

1list = []#Create an empty list2Flag = False#set a switch3INPUT1 = input ("plesae your input "www.oldboy1.org":")4With open ("haproxy.conf", encoding="Utf-8") as F_read:#Open the source file to F_read in utf-8 format5      forLineinchF_read:#traverse the file into line this variable6         ifLine.startswith ("Backend") andInput1inchLine :7Flag =True8             Continue  #jump out of this layer loop9 Ten         ifLine.startswith ("Backend"):#determines whether a string starts with backend One              Break A         ifflag: -List.append (Line.strip ())#Append to Empty list - Print("Hello". Center (50,"-")) the  -  forIinchList#Traverse List -     Print(i)


2. Delete:

Input: {"backend": "www.oldboy20.org", "record": {"Server": 1111, "weight": 2222, "Maxconn": 3333}}

1Choice = input ("your input is {}:")2     #{"Backend": "www.oldboy20.org", "record": {"Server": 1111, "Weight": 2222,maxconn:3333}}3DIC = eval (choice)#turn the input string into a dictionary4Tltle = dic["Backend"]5Tetle1 = dic["Record"]#it doesn't work .6With open ("haproxy.conf", encoding="Utf-8") as F_read, open ("Test5","W", encoding="Utf-8") as W_write:7 8          forLineinchF_read:9 W_write.write (line)Ten             ifLine.startswith ("Backend") andTltleinchLine : One                 #W_write.write (line) A                  Break

3. Increase:

Input: {"backend": "www.oldboy20.org", "record": {"Server": 1111, "weight": 2222, "Maxconn": 3333}}

1Choice = input ("your input is {}:")2     #{"Backend": "www.oldboy20.org", "record": {"Server": 1111, "weight": 2222, "Maxconn": 3333}}3DIC = eval (choice)#turn a string into a dictionary4title = dic['Backend']#"www.oldboy20.org"5Record = dic['Record']6r_s ='Server%s weight%s maxconn%s\n' 7% (record['Server'], record['Weight'], record['Maxconn'])8 9With open ("haproxy.conf", encoding="Utf-8") as F_read, open ("test3", mode="W", encoding="Utf-8") as W_read:Ten          forLineinchF_read: One W_read.write (line) A             ifLine.startswith ('Backend') andTitleinchLine : -                 #print (' 11111> ') - w_read.write (r_s) the                 #w_read.write (' =====> ') -W_read.flush ()

4. Modification:

Input: {"backend": "www.oldboy20.org", "record": {"Server": 1111, "weight": 2222, "Maxconn": 3333}}

1Choice = input ("your input is {}:")2     #{"Backend": "www.oldboy20.org", "record": {"Server": 1111, "weight": 2222, "Maxconn": 3333}}3DIC = eval (choice)#turn the input string into a dictionary4Tltel = dic["Backend"]5Tltel1 = dic["Record"]6dic["Record"]["Server"] ="0000"7dic["Record"]["Weight"] ="0000"8dic["Record"]["Maxconn"] ="0000"9     #print (DIC)Ten     #Formatting Strings Oner_s ="Server%s weight%s Maxconn%s"% (tltel1["Server"], tltel1['Weight'], tltel1["Maxconn"]) AWith open ("haproxy.conf", encoding="Utf-8") as F_read, open ("Test6", mode="W", encoding="Utf-8") as W_write: -          forLineinchF_read: -W_write.write (line)#write the string in line to W_write the             ifLine.startswith ("Backend") andTltelinchLine#determine if the string in line begins with "backend" and is full tetle -W_write.write (r_s)#writes a string to the W_rite -                  Break   #Jump out of the loop

Python-------file processing-------Job search

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.