Skip to content Skip to sidebar Skip to footer

How To Find And Replace Multiple Lines In Text File?

I am running Python 2.7. I have three text files: data.txt, find.txt, and replace.txt. Now, find.txt contains several lines that I want to search for in data.txt and replace that

Solution 1:

If the file is large, you want to read and writeone line at a time, so the whole thing isn't loaded into memory at once.

# create a dict of find keys and replace values
findlines = open('find.txt').read().split('\n')
replacelines = open('replace.txt').read().split('\n')
find_replace = dict(zip(findlines, replacelines))

withopen('data.txt') as data:
    withopen('new_data.txt', 'w') as new_data:
        for line in data:
            for key in find_replace:
                if key in line:
                    line = line.replace(key, find_replace[key])
            new_data.write(line)

Edit: I changed the code to read().split('\n') instead of readliens() so \n isn't included in the find and replace strings

Solution 2:

couple things here:

replace is not deprecated, see this discussion for details: Python 2.7: replace method of string object deprecated

If you are worried about reading data.txt in to memory all at once, you should be able to just iterate over data.txt one line at a time

data = open("data.txt", 'r')
for line indata:
    # fix the line

so all that's left is coming up with a whole bunch of find/replace pairs and fixing each line. Check out the zip function for a handy way to do that

find = open("find.txt", 'r').readlines()
replace = open("replace.txt", 'r').readlines()
new_data = open("new_data.txt", 'w')
for find_token, replace_token in zip(find, replace):
    new_line = line.replace(find_token, replace_token)
    new_data.write(new_line + os.linesep)

Post a Comment for "How To Find And Replace Multiple Lines In Text File?"