Python Forum
modify line in file if pattern found in list.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
modify line in file if pattern found in list.
#1
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.

# 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()
Reply
#2
Dedent line 41-42 to align the 'else' with the 'for'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 397 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  file open "file not found error" shanoger 8 938 Dec-14-2023, 08:03 AM
Last Post: shanoger
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,389 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Read text file, modify it then write back Pavel_47 5 1,499 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Getting last line of each line occurrence in a file tester_V 1 811 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,175 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Split string using variable found in a list japo85 2 1,235 Jul-11-2022, 08:52 AM
Last Post: japo85
  File not found error saisankalpj 10 3,697 Jul-04-2022, 07:57 AM
Last Post: saisankalpj
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,575 Apr-13-2022, 06:02 AM
Last Post: Paqqno

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020