Search code examples
pythonstringmp3id3

string.replace('the','') is leaving white space


I have a string that is the name of an artist that I get from the MP3 ID3 tag

sArtist = "The Beatles"

What I want is to change it to

sArtist = "Beatles, the"

I have running into 2 different problems. My first problem is that I seem to be trading 'The' for ''.

if sArtist.lower().find('the') == 0:
    sArtist = sArtist.lower().replace('the','')
    sArtist = sArtist + ", the"

My second problem is that since I have to check for both 'The' and 'the' I use sArtist.lower(). However this changes my result from " Beatles, the" to " beatles, the". To solve that problem I just removed the .lower and added a second line of code to explicitly look for both cases.

if sArtist.lower().find('the') == 0:
    sArtist = sArtist.replace('the','')
    sArtist = sArtist.replace('The','')
    sArtist = sArtist + ", the"

So the problem I really need to solve is why am I replacing 'the' with <SPACE> instead of <NULL>. But if somebody has a better way to do this I would be glad for the education :)


Solution

  • One way:

    >>> def reformat(artist,beg):
    ...   if artist.startswith(beg):
    ...     artist = artist[len(beg):] + ', ' + beg.strip()
    ...   return artist
    ...
    >>> reformat('The Beatles','The ')
    'Beatles, The'
    >>> reformat('An Officer and a Gentleman','An ')
    'Officer and a Gentleman, An'
    >>>