2016-07-18 7 views
0

Ich versuche zuerst aus der Datei zu lesen und dann in die Datei basierend auf der Benutzereingabe zu schreiben. Wenn es die Benutzereingabe findet, muss es weiter suchen, bis es den letzten Eintrag findet und es dann in der Zeile darunter hinzufügt. Im Moment kann es alle Einträge finden und darunter hinzufügen, aber es ist nicht möglich, nur den letzten Eintrag zu finden. Wie würde ich die gefundenen Einträge überspringen bis zum letzten Eintrag?Ändern einer Datei basierend auf der letzten gefundenen Übereinstimmung

import fileinput 


ldev_number = input("Enter ldev_number:") 
for line in fileinput.input(r'path', inplace=1): 

    line_data = line.strip() #removes line 
    print(line_data), #preserve old content 

    if ldev_number in line: 

     print(ldev_number) #add new data 

Antwort

0

Wahrscheinlich nicht der beste Weg, es zu tun, aber ich war in der Lage einer Benutzereingabe zu akzeptieren, und dann, wenn es eine Übereinstimmung (triggered_match) fand es den Zyklus zu sehen, wie viele Einträge gab es das entsprach den Text begonnen. Sobald die letzte Übereinstimmung gefunden wurde, wurde die vorherige Übereinstimmung auf "True" gesetzt, und "triggered_match" wurde auf "false" gesetzt, sodass Sie wussten, dass Sie am Ende der Treffer waren. Die zusätzliche Variable "first_match_only" stellt sicher, dass sie nur dann gedruckt wird, wenn sie zum ersten Mal eine Übereinstimmung in der Datei findet (falls am Ende der Datei mehr Übereinstimmungen vorhanden sind). Wenn die Übereinstimmung in der Datei nicht gefunden werden konnte, wird der Eintrag am Ende der Datei hinzugefügt.

import fileinput 
import re 

class Horcm(): 
    driver = None 
    def __init__(self): 

    group_name = "test_123" 
    device_name = "test_1234" 
    array_serial = "8998" 
    array_ldev = "3794" 
    mirror_unit = "0" 
    IP_address = "xxx.xxx.xxx.xxx" 
    horcm102 = "horcm102" 
    found_in_file= self.horcm_match(group_name, device_name, array_serial, array_ldev, mirror_unit) 

    self.add_horcm_inst(found_in_file, group_name,device_name, array_serial, array_ldev, mirror_unit, IP_address, horcm102) 

    def horcm_match(self,group_name, device_name, array_serial, array_ldev, mirror_unit): 
    # initalize variables 
    previous_match = False 
    first_time_matching_only = False 
    found_in_file = False  # used in add_horcm function 
    # search through file for matches 
    for line in fileinput.input(
      r'path', 
      inplace=1): 
     triggered_match = False # reset trigger 
     line_data = line.strip() # removes the extra line that is automatically added 
     if group_name in line: # If the user input is found in the file 
      found_in_file = True # determine if it needs to be added to file or not 
      triggered_match = True # set trigger 
      previous_match = triggered_match # store trigger 
     if previous_match == True: # If the previous line had a match 

      if triggered_match == False: # If the next line doesn't have a match 

       if first_time_matching_only == False: # if this is the first time finding a match 

        previous_match = False 
        first_time_matching_only = True # Don't allow it to print other matches 
        print(group_name+"  "+device_name+"  "+array_serial+"  " 
          +array_ldev+"  "+mirror_unit) # print the match below the last match 
     print(line_data), # preserve old content 
    return found_in_file 
    def add_horcm_inst(self,found_in_file,group_name,device_name, array_serial, array_ldev, mirror_unit, IP_address, horcm102): 
     if not found_in_file : # If you find it in the file there is no reason to add it to the file 
      input_filename = 'path' 
      with open(input_filename, 'a') as file1: #open the file 

       file1.write(group_name+"  "+IP_address+"  "+horcm102) #add horcm_instance to bottom of file in right syntax 

      with fileinput.FileInput(input_filename, inplace=True) as file: #open file again to add to the bottom of the "HORCM_LDEV" list 
       for line in file: # read line by line 
       file_syntax = '#/************************* For HORCM_INST ************************************/' #find the bottom of the list 
        print(line.replace(file_syntax, group_name+"  "+device_name+"  " 
        +array_serial+"  "+array_ldev+"  "+mirror_unit+'\n'+'\n'+file_syntax), end='') # add to list followed by file_syntax again 

def main(): 

    Horcm() 

if __name__ == '__main_' \ 
      '_': 
    Horcm() 
Verwandte Themen