根据找到的最后一个匹配修改文件

时间:2016-07-18 22:11:41

标签: python-3.x file-io

我尝试先从文件中读取,然后根据用户输入写入文件。如果找到用户输入,则需要继续查找,直到找到最后一个条目,然后将其添加到下面的行中。现在它可以找到所有条目并将其添加到它下面,但它无法找到最后一个条目。我怎样才能跳过它找到的条目,直到它到达最后一个条目?

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

1 个答案:

答案 0 :(得分:0)

可能不是最好的方法,但我能够接受用户输入,然后一旦找到匹配(triggered_match),它就开始循环以查看与文本匹配的条目数。一旦找到最后一个匹配的前一个匹配设置为True,则triggered_match设置为false,因此您知道自己位于匹配的底部。额外变量" first_match_only"确保它仅在第一次在整个文件中找到匹配时打印它(如果文件底部有更多匹配项)。如果它无法在文件中找到匹配项,则只需将该条目添加到文件的底部即可。

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()