ReXX Style translate() function for strings ?
Lucio Torre
lucio at movilogic.com
Tue Oct 30 16:13:10 EST 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
>
>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.
>
>TIA
>
>P. Adhia
>
This does that translation. surely it does not include the power of the
ReXX tranlations, but is pretty flexible.
def xlate(out, str, fmt):
result = [" "]*len(out)
for i in range(len(out)):
if out[i] in fmt:
pos = fmt.find(out[i])
result[i] = str[pos]
else:
result[i] = out[i]
return "".join(result)
More information about the Python-list
mailing list