469,646 Members | 1,496 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

string translate, replace, find and the forward slash

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.

Jun 27 '08 #1
4 4631
destroooooy <de*********@gmail.comwrites:
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
Jun 27 '08 #2
On Apr 29, 4:50 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
destroooooy <destrooo...@gmail.comwrites:
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.
Jun 27 '08 #3
On Apr 29, 4:50 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
destroooooy <destrooo...@gmail.comwrites:
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...
Jun 27 '08 #4
destroooooy <de*********@gmail.comwrites:
On Apr 29, 4:50 pm, Arnaud Delobelle <arno...@googlemail.comwrote:
>>
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
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by higabe | last post: by
32 posts views Thread by tshad | last post: by
3 posts views Thread by COHENMARVIN | last post: by
4 posts views Thread by Terry Olsen | last post: by
6 posts views Thread by Gabriel | last post: by
17 posts views Thread by Sumit | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.