find and replace multiple words in a file python

后端 未结 5 1848
生来不讨喜
生来不讨喜 2020-12-30 18:54

I took example code from here.

f1 = open(\'file1.txt\', \'r\')
f2 = open(\'file2.txt\', \'w\')
for line in f1:
    f2.write(line.replace(\'old_text\', \'new         


        
相关标签:
5条回答
  • 2020-12-30 19:14

    You can iterate over your check words and toReplace words using zip and then replace.

    Ex:

    checkWords = ("old_text1","old_text2","old_text3","old_text4")
    repWords = ("new_text1","new_text2","new_text3","new_text4")
    
    for line in f1:
        for check, rep in zip(checkWords, repWords):
            line = line.replace(check, rep)
        f2.write(line)
    f1.close()
    f2.close()
    
    0 讨论(0)
  • 2020-12-30 19:22

    It's easy use re module

    import re
    s = "old_text1 old_text2"
    s1 = re.sub("old_text" , "new_text" , s)
    

    output

    'new_text1 new_text2'

    re.sub substitute the old text with the new text re.sub doc https://docs.python.org/3.7/library/re.html#re.sub

    0 讨论(0)
  • 2020-12-30 19:25

    You can replace the content of a text or file with sub from the regex module (re):

    def replace_content(dict_replace, target):
        """Based on dict, replaces key with the value on the target."""
    
        for check, replacer in list(dict_replace.items()):
            target = sub(check, replacer, target)
    
        return target
    

    Or just with str.replace which does not need from re import sub:

    def replace_content(dict_replace, target):
        """Based on dict, replaces key with the value on the target."""
    
        for check, replacer in list(dict_replace.items()):
            target = target.replace(check, replacer)
    
        return target
    

    Here's the full implementation:

    from re import sub
    from os.path import abspath, realpath, join, dirname
    
    file = abspath(join(dirname(__file__), 'foo.txt'))
    file_open = open(file, 'r')
    file_read = file_open.read()
    file_open.close()
    
    new_file = abspath(join(dirname(__file__), 'bar.txt'))
    new_file_open = open(new_file, 'w')
    
    
    def replace_content(dict_replace, target):
        """Based on dict, replaces key with the value on the target."""
    
        for check, replacer in list(dict_replace.items()):
            target = sub(check, replacer, target)
            # target = target.replace(check, replacer)
    
        return target
    
    
    # check : replacer
    dict_replace = {
        'ipsum': 'XXXXXXX',
        'amet,': '***********',
        'dolor': '$$$$$'
    }
    
    new_content = replace_content(dict_replace, file_read)
    new_file_open.write(new_content)
    new_file_open.close()
    
    # Test
    print(file_read)
    # Lorem ipsum dolor sit amet, lorem ipsum dolor sit amet
    
    print(new_content)
    # Lorem XXXXXXX $$$$$ sit *********** lorem XXXXXXX $$$$$ sit amet
    
    0 讨论(0)
  • 2020-12-30 19:26

    I've learned that this script works very well and much faster then the ones that i've used in the past.

    import re
    
    def word_replace(text, replace_dict):
    rc = re.compile(r"[A-Za-z_]\w*")
    
    def translate(match):
        word = match.group(0).lower()
        print(word)
        return replace_dict.get(word, word)
    
    return rc.sub(translate, text)
    
    old_text = open('YOUR_FILE').read()
    
    replace_dict = {
    "old_word1" : 'new_word1',
    "old_word2" : 'new_word2',
    "old_word3" : 'new_word3',
    "old_word4" : 'new_word4',
    "old_word5" : 'new_word5'
    
     }                            # {"words_to_find" : 'word_to_replace'}
    
    output = word_replace(old_text, replace_dict)
    f = open("YOUR_FILE", 'w')                   #what file you want to write to
    f.write(output)                              #write to the file
    print(output)                                #check that it worked in the console 
    
    0 讨论(0)
  • 2020-12-30 19:37
    def replace_all(text, dic):
        for i, j in dic.iteritems():
            text = text.replace(i, j)
        return text
    

    Our method, replace_all(), takes 2 arguments. The first one, text, is the string or file (it’s text) that the replacement will take place. The second one, dic, is a dictionary with our word or character(s) to be replaced as the key, and the replacement word or character(s) as the value of that key. This dictionary can have just one key:value pair if you want to replace just one word or character, or multiple key:values if you want to replace multiple words or characters at once.

    Search and Replace multiple words or characters with Python

    0 讨论(0)
提交回复
热议问题