Jan-21-2020, 02:27 PM
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
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
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 |
#!/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) |