473,695 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.transl ate(maketransta ble(unsafe_char s, 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.partit ion("/")
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 4935
destroooooy <de*********@gm ail.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.transl ate(maketransta ble(unsafe_char s, 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.partit ion("/")
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_char s = "/#()[]!@$%^&*{}\'\"`? <>| \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)'.transla te(table)
'Jon__Mark_Stev e_'
>>>
--
Arnaud
Jun 27 '08 #2
On Apr 29, 4:50 pm, Arnaud Delobelle <arno...@google mail.comwrote:
destroooooy <destrooo...@gm ail.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.transl ate(maketransta ble(unsafe_char s, 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.partit ion("/")
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)'.transla te(table)
'Jon__Mark_Stev e_'

--
Arnaud

Oooh. Let me try it that way.
Jun 27 '08 #3
On Apr 29, 4:50 pm, Arnaud Delobelle <arno...@google mail.comwrote:
destroooooy <destrooo...@gm ail.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.transl ate(maketransta ble(unsafe_char s, 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.partit ion("/")
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)'.transla te(table)
'Jon__Mark_Stev e_'

--
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*********@gm ail.comwrites:
On Apr 29, 4:50 pm, Arnaud Delobelle <arno...@google mail.comwrote:
>>
marigold:jun k 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_char s = "/#()[]!@$%^&*{}\'\"`? <>| \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)'.transla te(table)
'Jon__Mark_Ste ve_'

--
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_char s = "/#()[]!@$%^&*{}\'\"`? <>| \t\n"
alt_chars = "______________ ___________"
table = string.maketran s(unsafe_chars, alt_chars)
"a/b/c".translate(ta ble)
'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
62103
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 supposed to write this function? String.replace(/</g,'&lt;');
32
14851
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
3
2023
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 & ";" objConnect = new OleDbConnection(strConnect) ============================= but I get the following error: <b>* Error while updating original data</b>.<br
16
5870
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 with the following elements arr(0) = "MyString" arr(1) = 40 arr(2) = "Hello world"
4
1744
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 messages with a forward slash "/" in the Message-ID. I specifically used a .Replace function to change the "/" to a "-". But is there a simple function to replace any non-filename-compliant character with a dash or other legal character?
6
6802
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 the code I use not work. Any idea ? Best Regards,
2
12618
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: open( FILE, "<$file" ); open( COPIED_FILE, ">$copiedfile" ); while( <FILE> ) { $templine = $_; if ($templine =~ m/DELTA\//)
14
21205
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 incorrect. Any ideas? I tried both convert... and datetime.par...
17
2319
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 when it get by http server it convert '/' to '%2F' i have to
0
8623
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8565
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9113
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8977
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7656
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6488
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5837
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2997
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.