Posts: 31
Threads: 20
Joined: Jun 2019
Hi
I want to write simple program in Python that replace the certain sequence of letters with some unicode symbol. For example, "delta_low" -> "δ". User defines input and output.
I have no idea how to implement it. What the place to start?
Posts: 4,787
Threads: 76
Joined: Jan 2018
I suggest the re module, especially the re.sub() function. This presentation could be a good starting point.
Posts: 7,319
Threads: 123
Joined: Sep 2016
Jan-15-2020, 07:27 PM
(This post was last modified: Jan-15-2020, 07:27 PM by snippsat.)
As Unicode is no problem in Python 3,start bye doing small test.
Example.
1 2 3 4 5 6 7 |
>>> sentence = 'Some measurement with delta_low did get get wrong'
>>> symbol = "δ"
>>> symbol_word = 'delta_low'
>>> if symbol_word in sentence:
... print (sentence.replace(symbol_word, symbol))
...
Some measurement with δ did get get wrong
|
If many symbols can eg using a dictionary,then replace as over but now using the dictionary.
1 2 3 4 |
>>> translation = { 'delta_low' : 'δ' , 'Epsilon' : 'ε' }
>>> translation[ 'Epsilon' ]
'ε'
>>>
|
Then can as @ Gribouillis suggests look a regex or translate functions,usage deepens on the project complexity.
Posts: 1,950
Threads: 8
Joined: Jun 2018
str.translate is for replacing character for character, string or None. So 'δ' -> 'delta_low' is possible, but 'delta_low' -> 'δ' is not.
1 2 3 4 5 6 |
>>> mapping = str .maketrans({ "δ" : 'delta_low' })
>>> 'Some measurement with δ did get get wrong' .translate(mapping)
'Some measurement with delta_low did get get wrong'
>>> mapping = str .maketrans({ 'delta_low' : "δ" })
/ .. /
ValueError: string keys in translate table must be of length 1
|
.maketrans documentation:
Quote:Character keys will be then converted to ordinals.
If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y.
.translate documentation:
Quote:Translation table, which must be a mapping of Unicode ordinals to Unicode ordinals, strings, or None.
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.
Posts: 31
Threads: 20
Joined: Jun 2019
but my question was not about the process of replacement itself. I know what ReExp is.
My question was about how to make a programm that run in background of OS and replace letters automatically when user it writes anywhere, for example, here, in this post.
Posts: 8,160
Threads: 160
Joined: Sep 2016
(Jan-16-2020, 08:18 AM)constantin01 Wrote: My question was about how to make a programm that run in background of OS and replace letters automatically when user it writes anywhere, for example, here, in this post.
nope, nothing of that kind in your original question :-)
(Jan-15-2020, 03:46 PM)constantin01 Wrote: Hi
I want to write simple program in Python that replace the certain sequence of letters with some unicode symbol. For example, "delta_low" -> "δ". User defines input and output.
I have no idea how to implement it. What the place to start?
|