Dec-10-2018, 08:39 AM
OS : window xp/7 | python 3
I'm new in python.
looking for help to modify file from a list from file.
master_f file:
!!!! 16 0 1 1526635345 0000
!IPG: rev 09.00p Fri May 18 17:22:26 2018
nodes "+1_5V"
nodes "+1_5V1"
nodes "GND"
nodes "+48V"
nodes "XSIG011083_12"
nodes "XSIG011083_121"
nodes "XSIG013829"
list_f file:
+1_5V
XSIG011083_12
XSIG013829
expected master file after modify:
!!!! 16 0 1 1526635345 0000
!IPG: rev 09.00p Fri May 18 17:22:26 2018
!p_rmv! nodes "+1_5V"
nodes "+1_5V1"
nodes "GND"
nodes "+48V"
!p_rmv! nodes "XSIG011083_12"
nodes "XSIG011083_121"
!p_rmv! nodes "XSIG013829"
Code i'm get from web , and modify some, but it it duplicate my line base on how many list im have, and it output to another file, and im need add "" at my list_f file.
I'm new in python.
looking for help to modify file from a list from file.
master_f file:
!!!! 16 0 1 1526635345 0000
!IPG: rev 09.00p Fri May 18 17:22:26 2018
nodes "+1_5V"
nodes "+1_5V1"
nodes "GND"
nodes "+48V"
nodes "XSIG011083_12"
nodes "XSIG011083_121"
nodes "XSIG013829"
list_f file:
+1_5V
XSIG011083_12
XSIG013829
expected master file after modify:
!!!! 16 0 1 1526635345 0000
!IPG: rev 09.00p Fri May 18 17:22:26 2018
!p_rmv! nodes "+1_5V"
nodes "+1_5V1"
nodes "GND"
nodes "+48V"
!p_rmv! nodes "XSIG011083_12"
nodes "XSIG011083_121"
!p_rmv! nodes "XSIG013829"
Code i'm get from web , and modify some, but it it duplicate my line base on how many list im have, and it output to another file, and im need add "" at my list_f file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# open the list of words to search for list_file = open ( 'master_f' ) search_words = [] # loop through the words in the search list for word in list_file: # save each word in an array and strip whitespace search_words.append(word.strip()) list_file.close() # this is where the matching lines will be stored matches = [] notmatchs = [] # open the master file master_file = open ( 'list_f' ) # create the new file new_file = open ( 'output' , 'w+' ) # loop through each line in the master file for line in master_file: # split the current line into array, this allows for us to use the "in" operator to search for exact strings current_line = line.split() # loop through each search word for search_word in search_words: # check if the search word is in the current line if search_word in current_line: # if found then save the line as we found it in the file matches.append(line) new_file.write( '!!p_rmv! ' + line) # once found then stop searching the current line break else : new_file.write(line) master_file.close() new_file.close() |