I'd like to convert a string of text, e.g., "User Name" into something which I can turn into part of a url, e.g., "User-Name." What's the fastest way to do a string replacement ("-" for " ") as well as make sure that characters are only [a-zA-Z0-9]?
views:
85answers:
3
+3
A:
string.translate is often the fastest solution for these sorts of problems (assuming your strings aren't unicode).
def translate(x):
if x == ' ': return '-'
if 'a' <= x <= 'z': return x
if 'A' <= x <= 'Z': return x
if '0' <= x <= '9': return x
def mk_translator():
translations = ''.join(translate(chr(c)) or chr(c) for c in xrange(256))
deletions = ''.join(chr(c) for c in xrange(256) if translate(chr(c)) is None)
return translations, deletions
def urlize(x, translator=mk_translator()):
return x.translate(*translator)
print urlize('User Name')
Paul Hankin
2010-04-03 17:06:24
I removed my answer since it will allow letters, digits and characters.
Anthony Forloney
2010-04-03 17:10:25
A:
I have used this function for that purpose:
import unicodedata
def slugify(item):
ret = item.lower().replace(u' ', u'_')
return unicodedata.normalize('NFKD', ret).encode('ascii', 'ignore')
I'm not sure if it's the fastest way, though.
bebraw
2010-04-03 17:10:57
A:
urllib.quote won't turn spaces to dashes, but to %20, but its designed exactly for the purpose of making string url-safe.
Ofri Raviv
2010-04-03 17:23:59