Thursday 15 April 2010

Deleting multiple lines based on serach conditon python -


I am working on a DHCP editor to add, delete, and search a host. I have a code that will search and add the host I thought I would add some to delete a host somehow and it will not work. What I want to do, I can make a list of Elements Index of f.readlines () , then use the values ​​of my MyDedax and then run lines.remove (myindex)

For example, I would like to search for foonode or any node and remove the following format:

  host burnload {option hosts} -name "birod"; Option root-path "0.0.0.0:/barnode"; Option subnet-mask; Option router; Hardware ethernet; Fixed address; } Host foonode {option host-name "foonode"; Option root-path "0.0.0.0:/foonode"; Option subnet-mask; Option router; Hardware ethernet; Fixed address; } Host foobarnode {option host-name "foobarnode"; Option root-path "0.0.0.0:/foobarnode"; Option subnet-mask; Option router; Hardware ethernet; Fixed address; }  

I can search the file using the following code:

  def delete_host (): host = raw_input ('Please enter that host What you want to remove: '); Start = wrong; F = open (infile, 'r') myfile = str () myindex = list () mystr = str () count = 0 lines = f.readlines () For lines in lines: If re.search (host, line) : Start = true if start: print line myindex = [lines.index (line)] if re.search ('}', line): break  

and get this output: / P>

  host fansos {option host name "afonod"; Option root-path "0.0.0.0:/foonode"; Option subnet-mask; Option router; Hardware ethernet; Fixed address;  

I wanted to create and index list in the search code for elements of f.readlines () element index values. Then using those values, to get the output out of a lines.remove (myindex.index ()) , I get it when I run the code, in fact, how do I search for any node and Remove them from the file? Perhaps creating an index is not the best way to solve this problem, I'm just going out of the expression to search in Google.

I know I have to do newfile = open ('/ var / tmp / foodhcp' 'w') but I want to correct the argument before starting to write the file.

Note that by removing items from a list you are looping the way you expect You can. I recommend that

  a = [1,2,3,4,5] del a [2: 4]  

a == [1 , 2], 5]

You are already able to find the start and finish line, so run Dell after finding those people.


No comments:

Post a Comment