473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4901
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
3
by: COHENMARVIN | last post by:
I put the following code in my asp.net page: dbPath = MapPath("/FBDB/db1_newport2003.mdb") strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" strConnect = strConnect & "Data Source=" & dbpath & ";"...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
4
by: Terry Olsen | last post by:
In my NNTP program, i'm using the Message-ID's as the filename (to eliminate duplicate messages coming in from different groups). My program has been working fine for months until I received some...
6
by: Gabriel | last post by:
Hello, I do this : s = Environment.CurrentDirectory; s = s.Replace("\\", @"\"); Environment.CurrentDirectiry return a path like this C:\\....\\....\\..... I'd like replace the \\ by \, but...
2
by: Perl Beginner | last post by:
Hi, Is there a way to find a line in a text file that starts with a forward slash? This is how i find a line in text file that starts with a word and then print that line to another text file: ...
14
by: Arjen | last post by:
Hi, I have a string with this value 07/11/2008. As you see no time included. How can I parse this string to a datetime variable? I tried to do this but I get the exception that the format is...
17
by: Sumit | last post by:
hi, I have a url which is a char * char * url = "/url=%2Fhome%2Fsumit%2Fpackages%2Fmyxml.xml"; when i send this URL to http server it was like - "/url=/home/sumit/pacakges/myxml.xml" but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.