Python: Translating Text

And they asked him, “Are you the one who is to come, or shall we look for another?” And Jesus answered them, “Go and tell John what you hear and see: the blind receive their sight and the lame walk, lepers are cleansed and the deaf hear, and the dead are raised up, and the poor have good news preached to them. Blessed is the one who is not offended by me.”

Matt 11:3-6

I'm a teacher by vocation and gifting; I love to teach. By corollary, I love to learn. I'm curious about everything and I have a blast learning about almost anything. Over the years, I've picked up quite a bit of information through the Internet. This website is my simple attempt to pay back; to contribute to the community and try to make the interwebs a better place.

As such, I don't require registration. I don't want your personal information. I don't set cookies. I won't put up advertisements. If I mention a product or service, it's because I use it, benefit from it, and I think others would too. I don't need, or want, your money. I simply want to offer some information to other curious characters.

I'm writing this site as an experiment. It's all written in a text editor. I make my own graphics, write my own JavaScript, CSS, PHP, and HTML from scratch because I'm trying to learn more about how all these pieces fit together. I use TextWrangler because I think it's a great text editor and it does everything I want in a very simple and efficient way.

If you want to contact me, try FaceBook or Twitter.

September 21, 2014

I've been spending more time lately trying to learn to script in Python the Python way (as a 'Pythonista' in the vernacular). I've decided to write a series of articles to document what I'm learning. Python is a great language for working with text and so for this, the first article in the series, I've decided to play around with translation as in simple ciphers.

The translate method is dependent upon the version of Python you use. At the time of this writing Python v3.4.1 is the current stable version and is what this article is based upon.

Translate is a very easy to use string method. You simply apply the method on the string you want to translate by passing the translation table and that's it. Here is an easy example:

#!/usr/local/bin/python3.4 message = 'gr\'q rfyr qgknjc.' print(message.translate(trans_tab))

Of course for this to actually work you need the translation table which is just a dictionary mapping Unicode numbers. Each entry of the dictionary just maps the Unicode number for a character to the Unicode number for another character. For example, the translation table {97: 99, 98: 100, 99: 101} would map 'a' to 'c', 'b' to 'd', and 'c' to 'e'. You can generate your own translation table dictionary or you can use the built-in maketrans method. It's also a very simple method to use and when you use the two methods together you can get a lot of work done fast. Here is the whole program to translate my little message:

#!/usr/local/bin/python3.4 trans_tab = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'cdefghijklmnopqrstuvwxyzab') message = 'gr\'q rfyr qgknjc.' print(message.translate(trans_tab))

Maketrans takes up to three arguments. The first is a string with the characters to translate, the second is a string of the same length where each character in the first string is mapped to the corresponding character in the second string. The third parameter is optional and is a string of characters to remove from the translation. The method returns a dictionary of numbers which represent the Unicode values of the characters from the two strings. Characters that aren't mapped or excluded are simply mapped to themselves.

Have fun playing with the translate method and I hope you find it interesting, if not useful.

All content and graphics copyright (c) 2012-2017 Brian Dentler - all rights reserved.