with open(r"test.log", encoding='utf-8') as f: # read data from TXT file list = [] for line1 in f: if line1 != '\n': # remove blank lines l = line1.split() # This sentence uses spaces to split the file content into character segments list.append(l) # Put l into the array for item in list: # Display the lines containing a certain string under a certain character field through a for loop if len(item) > 1 and item[1] == 'time': # Extract the line containing the time string. Use this to judge that some lines in the file are only one character long. f = open("time_test.txt", "a") # Store the extracted lines into a new file, and "a" stores data in an appended way f.write(item[6] + "\n")
Python extracts the lines containing a certain string in the file and writes them into a new file
Guess you like
Origin blog.csdn.net/qq_34474071/article/details/123790181
Recommended
Ranking