开发者

What's the fastest way to convert a string of text into a url-safe variable?

开发者 https://www.devze.com 2022-12-26 03:07 出处:网络
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 \" \")

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 o开发者_运维知识库nly [a-zA-Z0-9]?


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')


urllib.quote won't turn spaces to dashes, but to %20, but its designed exactly for the purpose of making string url-safe.


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.


I like Ofri's version for simplicity and safety, and user97370's version for making spaces look nice.

Why not have both?

I would do it like this:

import string, urllib
trans = string.maketrans(' ', '-')
x = 'a sentence with a bunch of spaces'
x2 = x.translate(trans)
x3 = urllib.quote(x2)
print x3 #--> 'a-sentence-with-a-bunch-of-spaces'

In other words, do one method and then the other. x3 should be safe to use in url string. You don't need to make new parameters for each, could just keep recreating x, i used x2 and x3 to make it clearer. You could also add other things in the translation matrix, if there are things other than spaces you want to get rid of.

0

精彩评论

暂无评论...
验证码 换一张
取 消