Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace words in a file
#1
I have problems with words with backslash in a given text that have a meaning in python. And characters inside words are unwantedly replaced as well. I get the output:

reak
as aes
a aes uplet { c ( aes a )}
ar

"\break" gives "reak", \tuplet gives "uplet" with an unwanted TAB in front because of \t, "\bar" becomes "ar".
"des" should become "bes", but becomes "aes" because of the the code the ' d' : ' a' in th dictionary, that is only meant for replacing single characters standing isolated.

Is there a solution? Modification of the original given text is not a solution.

Thanks

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 21 14:01:45 2020
@author: bb
"""
import re
 
def replace_words(text, word_dic):
    """
    take a text and replace words that match a key in a dictionary with
    the associated value, return the changed text
    """
    rc = re.compile('|'.join(map(re.escape, word_dic)))
    def translate(match):
        return word_dic[match.group(0)]
    return rc.sub(translate, text)
 
 
str1 = \
""" 
 \break
 es des
  d des \tuplet { c ( des d )}
  \bar 
"""
    
word_dict = {
' \tuplet' : ' \tuplet' ,
'\return' : '\return' ,
'\break' : '\break' ,
'\bar' : '\bar ' ,
' es' : ' as' ,
' d' : ' a' ,
'des' : 'bes'
}

str2 = replace_words(str1, word_dict)
 
# test
print (str2)
Reply
#2
How you escape backslash? With another backslash Smile

str1 = \ 
r"""                                         # r for raw string
 \break 
  es des 
   d des \tuplet { c ( des d )} 
   \bar  
"""                                                              
>>> str1                                                             
' \n \\break\n es des\n  d des \\tuplet { c ( des d )}\n  \\bar \n'
>>> str1.replace('\\break', '\\car')                                 
' \n \\car\n es des\n  d des \\tuplet { c ( des d )}\n  \\bar \n'
>>> print(str1.replace('\\break', '\\car'))                          

 \car
 es des
  d des \tuplet { c ( des d )}
  \bar 
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
not exactly answer to your question, but looking at the first 3 key:value pairs key and value are identical....
Also because \b, \t, \r are escape sequences, they are "translated" accordingly when print the respective string, e.g. \t becomes TAB
so you need to print(repr(str2)) to see what the actual result of your function is
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thanks. I know and have described the \-problem in my first mail. But an idea arose reading the responses. I might double the "\" to a "\\" by a simple document editor by replacing all "\" by "\\" and feed the resulting modified text into the program. I will report if it works soon. (I have some other problems to work with right now.)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 3,304 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 761 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Replace columns indexes reading a XSLX file Larry1888 2 978 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  How to do a mass replace within a CSV file? cubangt 9 5,354 May-09-2022, 06:52 PM
Last Post: snippsat
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,400 May-06-2022, 01:44 PM
Last Post: Larz60+
  Extract a string between 2 words from a text file OscarBoots 2 1,869 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,792 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Cloning a directory and using a .CSV file as a reference to search and replace bg25lam 2 2,133 May-31-2021, 07:00 AM
Last Post: bowlofred
  Counting the most relevant words in a text file caiomartins 2 2,477 Sep-21-2020, 08:39 AM
Last Post: caiomartins
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,825 Aug-10-2020, 11:01 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

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