Python Forum
Write text expander in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write text expander in Python
#1
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?
Reply
#2
I suggest the re module, especially the re.sub() function. This presentation could be a good starting point.
Reply
#3
As Unicode is no problem in Python 3,start bye doing small test.
Example.
>>> 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.
>>> translation = {'delta_low': 'δ', 'Epsilon': 'ε'}
>>> translation['Epsilon']
'ε'
>>> 
Then can as @Gribouillis suggests look a regex or translate functions,usage deepens on the project complexity.
Reply
#4
str.translate is for replacing character for character, string or None. So 'δ' -> 'delta_low' is possible, but 'delta_low' -> 'δ' is not.

>>> 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.
Reply
#5
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.
Reply
#6
(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?
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Read text file, modify it then write back Pavel_47 5 1,500 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  How to write in text file - indented block Joni_Engr 4 6,362 Jul-18-2022, 09:09 AM
Last Post: Hathemand
  CSV to Text File and write a line in newline atomxkai 4 2,612 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  Is there a way to move text created by turtle.write()? derekered 7 5,824 Dec-16-2020, 09:44 AM
Last Post: itexamples
  List Won't Write in Text File IILawrenceII 4 2,098 Jul-17-2020, 11:16 PM
Last Post: j.crater
  read text file and write into html with correct link jacklee26 4 2,861 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  write image into string format into text file venkat18 2 4,353 Jun-01-2019, 06:46 AM
Last Post: venkat18
  write each line of a text file into separate text files and save with different names Shameendra 3 2,744 Feb-20-2019, 09:07 AM
Last Post: buran
  How to display what I write on sublime text editor on the CMD el_bueno 5 3,139 Feb-04-2019, 02:00 PM
Last Post: buran
  How to write a code to open and read text file by clicking on the "text file" kavindu 4 4,074 Jul-06-2018, 06:55 PM
Last Post: buran

Forum Jump:

User Panel Messages

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