string translate, replace, find and the forward slash

Discussion in 'Python' started by destroooooy, Apr 29, 2008.

  1. destroooooy

    destroooooy Guest

    Hi folks,
    I'm finding some (what I consider) curious behavior with the string
    methods and the forward slash character. I'm writing a program to
    rename mp3 files based on their id3 tags, and I want to protect
    against goofy characters in the in tags. So I do the following:

    unsafe_chars = "/#()[]!@$%^&*{}\'\"`?<>| \t\n"
    alt_chars = "_________________________"

    s_artist.translate(maketranstable(unsafe_chars, alt_chars))


    which successfully replaces everything except for forward slashes (at
    least in the files I've tested so far). If I use the "replace()"
    method, it also does not work. Escaping the forward slash changes
    nothing. "find()" however, works, and thus I've resorted to:

    if "/" in s_artist:
    (s_l, slash, s_r) = s_artist.partition("/")
    s_artist = "_".join([s_l, s_r])

    which is rather uncool. It works but I'd just like to know what the
    deal is. TIA.
     
    destroooooy, Apr 29, 2008
    #1
    1. Advertising

  2. destroooooy <> writes:

    > Hi folks,
    > I'm finding some (what I consider) curious behavior with the string
    > methods and the forward slash character. I'm writing a program to
    > rename mp3 files based on their id3 tags, and I want to protect
    > against goofy characters in the in tags. So I do the following:
    >
    > unsafe_chars = "/#()[]!@$%^&*{}\'\"`?<>| \t\n"
    > alt_chars = "_________________________"
    >
    > s_artist.translate(maketranstable(unsafe_chars, alt_chars))
    >
    >
    > which successfully replaces everything except for forward slashes (at
    > least in the files I've tested so far). If I use the "replace()"
    > method, it also does not work. Escaping the forward slash changes
    > nothing. "find()" however, works, and thus I've resorted to:
    >
    > if "/" in s_artist:
    > (s_l, slash, s_r) = s_artist.partition("/")
    > s_artist = "_".join([s_l, s_r])
    >
    > which is rather uncool. It works but I'd just like to know what the
    > deal is. TIA.


    It works fine here:

    marigold:junk arno$ python
    Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
    [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> unsafe_chars = "/#()[]!@$%^&*{}\'\"`?<>| \t\n"
    >>> table = range(256)
    >>> for c in unsafe_chars: table[ord(c)] = ord('_')

    ....
    >>> table = ''.join(chr(o) for o in table)
    >>> 'Jon(&Mark/Steve)'.translate(table)

    'Jon__Mark_Steve_'
    >>>


    --
    Arnaud
     
    Arnaud Delobelle, Apr 29, 2008
    #2
    1. Advertising

  3. destroooooy

    destroooooy Guest

    On Apr 29, 4:50 pm, Arnaud Delobelle <> wrote:
    > destroooooy <> writes:
    > > Hi folks,
    > > I'm finding some (what I consider) curious behavior with the string
    > > methods and the forward slash character. I'm writing a program to
    > > rename mp3 files based on their id3 tags, and I want to protect
    > > against goofy characters in the in tags. So I do the following:

    >
    > > unsafe_chars = "/#()[]!@$%^&*{}\'\"`?<>| \t\n"
    > > alt_chars = "_________________________"

    >
    > > s_artist.translate(maketranstable(unsafe_chars, alt_chars))

    >
    > > which successfully replaces everything except for forward slashes (at
    > > least in the files I've tested so far). If I use the "replace()"
    > > method, it also does not work. Escaping the forward slash changes
    > > nothing. "find()" however, works, and thus I've resorted to:

    >
    > > if "/" in s_artist:
    > > (s_l, slash, s_r) = s_artist.partition("/")
    > > s_artist = "_".join([s_l, s_r])

    >
    > > which is rather uncool. It works but I'd just like to know what the
    > > deal is. TIA.

    >
    > It works fine here:
    >
    > marigold:junk arno$ python
    > Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
    > [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    > >>> unsafe_chars = "/#()[]!@$%^&*{}\'\"`?<>| \t\n"
    > >>> table = range(256)
    > >>> for c in unsafe_chars: table[ord(c)] = ord('_')

    > ...
    > >>> table = ''.join(chr(o) for o in table)
    > >>> 'Jon(&Mark/Steve)'.translate(table)

    > 'Jon__Mark_Steve_'
    >
    > --
    > Arnaud



    Oooh. Let me try it that way.
     
    destroooooy, Apr 29, 2008
    #3
  4. destroooooy

    destroooooy Guest

    On Apr 29, 4:50 pm, Arnaud Delobelle <> wrote:
    > destroooooy <> writes:
    > > Hi folks,
    > > I'm finding some (what I consider) curious behavior with the string
    > > methods and the forward slash character. I'm writing a program to
    > > rename mp3 files based on their id3 tags, and I want to protect
    > > against goofy characters in the in tags. So I do the following:

    >
    > > unsafe_chars = "/#()[]!@$%^&*{}\'\"`?<>| \t\n"
    > > alt_chars = "_________________________"

    >
    > > s_artist.translate(maketranstable(unsafe_chars, alt_chars))

    >
    > > which successfully replaces everything except for forward slashes (at
    > > least in the files I've tested so far). If I use the "replace()"
    > > method, it also does not work. Escaping the forward slash changes
    > > nothing. "find()" however, works, and thus I've resorted to:

    >
    > > if "/" in s_artist:
    > > (s_l, slash, s_r) = s_artist.partition("/")
    > > s_artist = "_".join([s_l, s_r])

    >
    > > which is rather uncool. It works but I'd just like to know what the
    > > deal is. TIA.

    >
    > It works fine here:
    >
    > marigold:junk arno$ python
    > Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
    > [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
    > Type "help", "copyright", "credits" or "license" for more information.
    >
    > >>> unsafe_chars = "/#()[]!@$%^&*{}\'\"`?<>| \t\n"
    > >>> table = range(256)
    > >>> for c in unsafe_chars: table[ord(c)] = ord('_')

    > ...
    > >>> table = ''.join(chr(o) for o in table)
    > >>> 'Jon(&Mark/Steve)'.translate(table)

    > 'Jon__Mark_Steve_'
    >
    > --
    > Arnaud



    Okay, so that definitely works. Thanks!

    However, the chances of me coming up with that on my own were
    completely nonexistent, and I'd still like to know how one would use
    maketranstable() to get the same result...
     
    destroooooy, Apr 29, 2008
    #4
  5. destroooooy <> writes:

    > On Apr 29, 4:50 pm, Arnaud Delobelle <> wrote:
    >>
    >> marigold:junk arno$ python
    >> Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
    >> [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
    >> Type "help", "copyright", "credits" or "license" for more information.
    >>
    >> >>> unsafe_chars = "/#()[]!@$%^&*{}\'\"`?<>| \t\n"
    >> >>> table = range(256)
    >> >>> for c in unsafe_chars: table[ord(c)] = ord('_')

    >> ...
    >> >>> table = ''.join(chr(o) for o in table)
    >> >>> 'Jon(&Mark/Steve)'.translate(table)

    >> 'Jon__Mark_Steve_'
    >>
    >> --
    >> Arnaud

    >
    >
    > Okay, so that definitely works. Thanks!
    >
    > However, the chances of me coming up with that on my own were
    > completely nonexistent, and I'd still like to know how one would use
    > maketranstable() to get the same result...


    Do you mean maketrans() from the string module? I didn't know about
    it, but I've tried it and it works too:

    >>> import string
    >>> unsafe_chars = "/#()[]!@$%^&*{}\'\"`?<>| \t\n"
    >>> alt_chars = "_________________________"
    >>> table = string.maketrans(unsafe_chars, alt_chars)
    >>> "a/b/c".translate(table)

    'a_b_c'
    >>>


    --
    Arnaud
     
    Arnaud Delobelle, Apr 29, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AviraM
    Replies:
    2
    Views:
    6,505
    Manish Pandit
    Sep 28, 2006
  2. harryos
    Replies:
    2
    Views:
    1,750
  3. Chris Rebert

    Re: string replace for back slash

    Chris Rebert, Feb 5, 2009, in forum: Python
    Replies:
    0
    Views:
    439
    Chris Rebert
    Feb 5, 2009
  4. Replies:
    0
    Views:
    2,076
  5. Jeff Mitchell

    trailing slash issue in Find.find

    Jeff Mitchell, Aug 24, 2003, in forum: Ruby
    Replies:
    0
    Views:
    112
    Jeff Mitchell
    Aug 24, 2003
Loading...

Share This Page