J
James Stroud
Hello all,
I dashed off the following function to clean a string in a little
program I wrote:
def cleanup(astr, changes):
for f,t in changes:
atr = astr.replace(f, t)
return astr
where changes would be a tuple, for example:
changes = (
('%', '\%'),
('$', '\$'),
('-', '_')
)
If these were were single replacements (like the last), string.translate
would be the way to go. As it is, however, the above seems fairly
inefficient as it potentially creates a new string at each round. Does
some function or library exist for these types of transformations that
works more like string.translate or is the above the best one can hope
to do without writing some C? I'm guessing that "if s in astr" type
optimizations are already done in the replace() method, so that is not
really what I'm getting after.
James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com/
I dashed off the following function to clean a string in a little
program I wrote:
def cleanup(astr, changes):
for f,t in changes:
atr = astr.replace(f, t)
return astr
where changes would be a tuple, for example:
changes = (
('%', '\%'),
('$', '\$'),
('-', '_')
)
If these were were single replacements (like the last), string.translate
would be the way to go. As it is, however, the above seems fairly
inefficient as it potentially creates a new string at each round. Does
some function or library exist for these types of transformations that
works more like string.translate or is the above the best one can hope
to do without writing some C? I'm guessing that "if s in astr" type
optimizations are already done in the replace() method, so that is not
really what I'm getting after.
James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com/