ReXX Style translate() function for strings ?

Ype Kingma ykingma at accessforall.nl
Tue Oct 30 23:14:12 CET 2001


P Adhia wrote:
> 
> Hello,
> 
> I am a newbie to python, and remain impressed with the power of the
> language overall. There are however some little features that I am
> used to in ReXX (default scripting language for mainframes) that I
> could not find in python.
> 
> ReXX has a translate() function that is much more powerful (and I
> think coule be a nice addition to python; of course with a different
> name)
> 
> e.g. in ReXX, to change a date from yyyymmdd to mm/dd/yyyy is
> trivially simple,
> new_date = Translate('56/78/1234', old_date, '12345678')
> 
> for formal syntax refert to,
> http://users.comlab.ox.ac.uk/ian.collier/Docs/rexx_ref/F/BI
> 

There is string method in python:

translate(table[, deletechars])
      Return a copy of the string where all characters occurring in the optional
      argument deletechars are removed, and the
      remaining characters have been mapped through the given translation table, which
      must be a string of length 256. 

which is not quite what you are looking for.
The REXX translate is more powerful than this. May be it is possible to get
the same behaviour using two translations in series with carefully computed
tables.
A more python-like way of changing the date format of a string could be:

new_date = ''.join(old_date[4:5],'/',old_date[6:7],'/',old_date[0:3])

The time module also contains the strftime() method that will do
almost anything you want with date formats.

> Python's l/r/strip() functions always remove white spaces --
> corresponding ReXX functions (actually only one) has a optional
> argument that defines what character to strip. e.g. removing leading
> '0' from a number, punctualtion characters etc.
> 
> If such functionality is trivial in python, I'll appreciate any new
> addition to my growing python knowledge.
> 

Removing leading zero's is bit more tricky, but normally
str(long(yournumberstring)) will do.

REXX has PARSE, python has regular expressions: more complicated and
even more powerful.

The way back to REXX is uphill...

Have fun,
Ype


-- 
email at xs4all.nl



More information about the Python-list mailing list