[Tutor] regexes, thanks
Matt
matt@ipwib.net
Tue Nov 19 11:21:02 2002
I was actually wanting to take a list of filenames, for example:
Pink Floyd - 02 - Dogs.mp3
Japanese_Noh_Music_--_Gaku.mp3
asu - searching_2001_edit.MP3
...and pass it through a sequence of regexes (of which this is just one)
to produce consistent filenames along the lines of:
Pink_Floyd-02-Dogs.mp3
Japanese_Noh_Music--Gaku.mp3
Asu--Searching_2001_Edit.mp3
I first read in the filenames, then produce a list of tuples [filename,
new_filename], which are used for renaming at the end. So, I need to
perform operations on certain parts of the filename, not just the whole
thing. It seems that there would be a simple way to perform this stage.
Like,
new_filename = words.sub('([a-zA-Z]+)', '\u\1', filename)
But I couldn't get \1, \2 substitution working (had to use '\g<1>') and
it doesn't look like python supports \u and \U ('man perlre' for info),
so that I couldn't just put '\u\1' in the substitution string. I'm just
wondering if what I came up with is the most efficient method for
performing what I need at this stage. Anyway...
Thanks for the help. I hadn't looked at globs before, so that will be
very helpful.
-Matt
lumbricus@gmx.net wrote:
[snip!]
>>
>> def upper(a):
>> return(string.capitalize(a.string[a.start():a.end()]))
>>
>> words = re.compile('([a-zA-Z]+)')
>> new_filename = words.sub(upper, filename)
>
>
> Something along
>
> import glob
> for f in glob.glob("*.mp3"):
> new=f.upper()
>
> might be what you want?
>
> HTH, J"o!