making a valid file name...

Fabio Chelly fabio at NOSPAMchelly.net
Wed Oct 18 08:06:32 EDT 2006


You should use the s.translate()
It's 100x faster:

#Creates the translation table
ValidChars = ":./,^0123456789abcdefghijklmnopqrstuvwxyz"
InvalidChars = "".join([chr(i) for i in range(256) if not 
chr(i).lower() in ValidChars])
TranslationTable = "".join([chr(i) for i in range(256)])

def valid_filename(fname):
  return fname.translate(TranslationTable, InvalidChars)

>> valid =
>> ':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '
>> 
>> if I have a string called fname I want to go through each character in
>> the filename and if it is not a valid character, then I want 
>> to replace
>> it with a space.

-- 
Ceci est une signature automatique de MesNews.
Site : http://www.mesnews.net





More information about the Python-list mailing list