FAQ
string.translate can delete characters from a string, as well as
translate them. But what if I want *only* to delete them? Currently
I do this:

dummytable = string.maketrans('', '')

...
host = string.translate(host, dummytable, ' "')
...

What I would really like to be able to do is write:

host = string.translate(host, None, ' "')

Am I missing a more elegant solution?

Search Discussions

  • Aahz Maruch at Jun 5, 1999 at 4:27 pm
    In article <87n1ye3g4v.fsf at pc-hrvoje.srce.hr>,
    Hrvoje Niksic wrote:
    What I would really like to be able to do is write:

    host = string.translate(host, None, ' "')

    Am I missing a more elegant solution?
    I dunno, but I'd tend to use re.sub().
    --
    --- Aahz (@netcom.com)

    Hugs and backrubs -- I break Rule 6 <*> http://www.rahul.net/aahz/
    Androgynous poly kinky vanilla queer het

    "Why do you like my boyfriend to tie you up and beat me.?"
    -- ELIZA generates a poly moment
  • Hrvoje Niksic at Jun 5, 1999 at 4:43 pm

    aahz at netcom.com (Aahz Maruch) writes:

    In article <87n1ye3g4v.fsf at pc-hrvoje.srce.hr>,
    Hrvoje Niksic wrote:
    What I would really like to be able to do is write:

    host = string.translate(host, None, ' "')

    Am I missing a more elegant solution?
    I dunno, but I'd tend to use re.sub().
    I should have made it clear, sorry. I don't want want to import re
    because I don't need it anywhere else in my program. It's even less
    elegant and probably much slower than string.translate() anyway.
  • Tim Peters at Jun 5, 1999 at 4:56 pm
    [Hrvoje Niksic]
    string.translate can delete characters from a string, as well as
    translate them. But what if I want *only* to delete them? Currently
    I do this:

    dummytable = string.maketrans('', '')

    ...
    host = string.translate(host, dummytable, ' "')
    ...

    What I would really like to be able to do is write:

    host = string.translate(host, None, ' "')
    That's easy; change your assignment to

    None = string.maketrans('', '')

    and you can write exactly that <wink>.

    It's (yours, not mine ...) a decent idea; in the meantime how about writing
    a little wrapper

    def deletechars(s, todelete, id=string.maketrans('', '')):
    return string.translate(s, id, todelete)

    Then you can use an interface that doesn't bother you with arguments you
    don't care about. Or you can borrow the string module's private spelling,

    host = string.translate(host, string._idmap, ' "')

    Maybe good too if _idmap lost its leading underscore -- it's been in the
    module forever, and is exactly the "don't translate anything" argument you
    need.

    tweakingly y'rs - tim
  • Hrvoje Niksic at Jun 5, 1999 at 6:30 pm

    "Tim Peters" <tim_one at email.msn.com> writes:

    It's (yours, not mine ...) a decent idea; in the meantime how about
    writing a little wrapper

    def deletechars(s, todelete, id=string.maketrans('', '')):
    return string.translate(s, id, todelete)
    I think I'd rather write a patch that allows None as the second
    argument to string.translate(), if that's OK with the developers.

    Thanks for the tip, though.
  • Magnus L. Hetland at Jun 5, 1999 at 7:51 pm

    Hrvoje Niksic <hniksic at srce.hr> writes:

    string.translate can delete characters from a string, as well as
    translate them. But what if I want *only* to delete them? Currently
    I do this:

    dummytable = string.maketrans('', '')

    ...
    host = string.translate(host, dummytable, ' "')
    ...

    What I would really like to be able to do is write:

    host = string.translate(host, None, ' "')
    Just to butt in with my private opinion... :) I think it would be more
    natural if bot the last two arguments were optional... Thus you could
    do:

    host = string.translate(host, deletechars=' "')

    To me that is more readable, and seems more like standard Python than
    defining None to mean an ID-mapping... Just use an ID-mapping as a
    default argument in string.translate.
    Am I missing a more elegant solution?
    result = ""
    for char in host:
    if char not in ' "':
    result = result+char

    host = result


    What!? To me that's more elegant... <wink>

    --

    Magnus Making no sound / Yet smouldering with passion
    Lie The firefly is still sadder / Than the moaning insect
    Hetland : Minamoto Shigeyuki
  • Hrvoje Niksic at Jun 5, 1999 at 7:56 pm

    mlh at idt.ntnu.no (Magnus L. Hetland) writes:

    result = ""
    for char in host:
    if char not in ' "':
    result = result+char

    host = result

    What!? To me that's more elegant... <wink>
    To me it would be if Python actually had chars. <handshake>

    :-)
  • Magnus L. Hetland at Jun 6, 1999 at 10:47 pm

    Hrvoje Niksic <hniksic at srce.hr> writes:

    mlh at idt.ntnu.no (Magnus L. Hetland) writes:
    result = ""
    for char in host:
    if char not in ' "':
    result = result+char

    host = result

    What!? To me that's more elegant... <wink>
    To me it would be if Python actually had chars. <handshake>
    What do you mean? The example above works...
    :-)
    --

    Magnus Making no sound / Yet smouldering with passion
    Lie The firefly is still sadder / Than the moaning insect
    Hetland : Minamoto Shigeyuki
  • Hrvoje Niksic at Jun 7, 1999 at 11:00 am

    mlh at idt.ntnu.no (Magnus L. Hetland) writes:

    What!? To me that's more elegant... <wink>
    To me it would be if Python actually had chars. <handshake>
    What do you mean? The example above works...
    I mean that to me it's not elegant at all. Breaking up strings into
    more strings is anything but elegant.

    Just forget it, I guess. It was an attempt at humor.
  • Aahz Maruch at Jun 5, 1999 at 8:32 pm
    In article <y0jyahye89i.fsf at vier.idi.ntnu.no>,
    Magnus L. Hetland wrote:
    result = ""
    for char in host:
    if char not in ' "':
    result = result+char

    host = result
    That's a bad way to do it. At the very least, that code should be
    something like:

    # There may be a couple of syntax errors below
    result = []
    for char in host:
    if char not in ' "':
    result.append(char)
    host = string.join ( result, "" )

    This has *much* better performance if host is at all long.
    --
    --- Aahz (@netcom.com)

    Hugs and backrubs -- I break Rule 6 <*> http://www.rahul.net/aahz/
    Androgynous poly kinky vanilla queer het

    "Why do you like my boyfriend to tie you up and beat me.?"
    -- ELIZA generates a poly moment
  • Hans Nowak at Jun 5, 1999 at 9:45 pm

    On 5 Jun 99, Hrvoje Niksic wrote:

    string.translate can delete characters from a string, as well as
    translate them. But what if I want *only* to delete them? Currently
    I do this:

    dummytable = string.maketrans('', '')

    ...
    host = string.translate(host, dummytable, ' "')
    ...

    What I would really like to be able to do is write:

    host = string.translate(host, None, ' "')

    Am I missing a more elegant solution?
    Hmm... how about

    host = filter(lambda c: not c in ' "', host)

    Depends on how much you like, or hate, functional idiom, though. :)

    --Hans Nowak (ivnowa at hvision.nl)
    Homepage: http://fly.to/zephyrfalcon
  • Hrvoje Niksic at Jun 6, 1999 at 1:09 pm

    "Hans Nowak" <ivnowa at hvision.nl> writes:

    Hmm... how about

    host = filter(lambda c: not c in ' "', host)

    Depends on how much you like, or hate, functional idiom, though. :)
    I adore functional idioms (I also use the various Lisps), but I don't
    adore their slowness in Python so I avoid using them.
  • Magnus L. Hetland at Jun 6, 1999 at 10:49 pm

    "Hans Nowak" <ivnowa at hvision.nl> writes:

    On 5 Jun 99, Hrvoje Niksic wrote: [...]
    host = filter(lambda c: not c in ' "', host)
    Or - IMO more appropriately <wink>:

    host = filter(lambda c: c not in ' "', host)
    Depends on how much you like, or hate, functional idiom, though. :)
    --

    Magnus Making no sound / Yet smouldering with passion
    Lie The firefly is still sadder / Than the moaning insect
    Hetland : Minamoto Shigeyuki
  • Skip Montanaro at Jun 6, 1999 at 7:57 pm

    What I would really like to be able to do is write:

    host = string.translate(host, None, ' "')

    Am I missing a more elegant solution?
    Probably less elegant, but you can use string.replace. You'll have to
    call it twice to get the spaces and the double quotes:

    host = string.replace(string.replace(host, '"', ""), " ", "")

    --
    Skip Montanaro | Mojam: "Uniting the World of Music"
    http://www.mojam.com/
    skip at mojam.com | Musi-Cal: http://www.musi-cal.com/
    518-372-5583
  • Hrvoje Niksic at Jun 6, 1999 at 8:09 pm

    Skip Montanaro <skip at mojam.com> writes:

    What I would really like to be able to do is write:

    host = string.translate(host, None, ' "')

    Am I missing a more elegant solution?
    Probably less elegant, but you can use string.replace. You'll have to
    call it twice to get the spaces and the double quotes:

    host = string.replace(string.replace(host, '"', ""), " ", "")
    The problem with this solution is that it requires calling
    string.replace() as many times as there are characters to be deleted.
    This would not be a problem if not for the fact that string.replace()
    creates a new string every time around. So for many characters
    string.translate() should be much more efficient.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 5, '99 at 1:56p
activeJun 7, '99 at 11:00a
posts15
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase