gpt4 book ai didi

Python:如何使用 string.translate() 替换引号? (用于 "slug"创建)

转载 作者:行者123 更新时间:2023-11-28 21:27:52 25 4
gpt4 key购买 nike

我想从字符串中删除所有奇怪的字符以使其“url 安全”。因此,我有一个功能是这样的:

def urlize(url, safe=u''):
intab = u"àáâãäåòóôõöøèéêëçìíîïùúûüÿñ" + safe
outtab = u"aaaaaaooooooeeeeciiiiuuuuyn" + safe
trantab = dict((ord(a), b) for a, b in zip(intab, outtab))
return url.lower().translate(trantab).strip()

这很好用,但现在我想重用该函数以允许使用特殊字符。例如,引号。

urlize(u'This is sóme randóm "text" that í wánt to process',u'"')

...并抛出以下错误:

Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: expected a character buffer object

我试过了,但是没有成功:

urlize(u'text',u'\"')
intab = u"àáâãäåòóôõöøèéêëçìíîïùúûüÿñ%s" , safe

--编辑--完整的功能看起来像这样

def urlize(url, safe=u''):

intab = u"àáâãäåòóôõöøèéêëçìíîïùúûüÿñ" + safe
outtab = u"aaaaaaooooooeeeeciiiiuuuuyn" + safe
trantab = dict((ord(a), b) for a, b in zip(intab, outtab))
translated_url = url.lower().translate(trantab).strip()

pos = 0
stop = len(translated_url)
new_url= ''
last_division_char = False

while pos < stop:
if not translated_url[pos].isalnum() and translated_url[pos] not in safe:
if (not last_division_char) and (pos != stop -1):
new_url+='-'
last_division_char = True
else:
new_url+=translated_url[pos]
last_division_char = False
pos+=1

return new_url

--编辑--目标

我想要的是规范化文本,以便我可以自己将其放在 url 上,并像 Id 一样使用它。例如,如果我想展示某个类别的产品,我宁愿使用“ninos-y-bebes”而不是“niños-y-bebés”( child 和婴儿的西类牙语)。我真的不想在我的网址中使用所有 áéíóúñ(西类牙语中的特殊字符),但我也不想摆脱它们。这就是为什么我想替换所有看起来相同的字符(不是 100% 所有字符,我不在乎),然后删除所有非字母数字字符。

最佳答案

unidecode 模块是一个更安全的选择(它将处理其他特殊符号,如“degree”):

>>> from unidecode import unidecode
>>> s = u'This is sóme randóm "text" that í wánt to process'
>>> unidecode(s)
'This is some random "text" that i want to process'
>>> import urllib
>>> urllib.urlencode(dict(x=unidecode(s)))[2:]
'This+is+some+random+%22text%22+that+i+want+to+process'

[更新]

i think i'm already doing that -> u"aaaaaaooooooeeeeciiiiuuuuyn" – Marco Bruggmann

很公平,如果您愿意为您的翻译表跟踪每个 unicode 字符(重音字符不是唯一的问题,还有大量的符号会在您的游行中下雨)。

最糟糕的是,许多 unicode 符号在视觉上可能与其对应的 ASCII 符号完全相同,从而导致难以诊断错误。

[更新]

比如:

>>> safe_chars = 'abcdefghijklmnopqrstuvwxyz01234567890-_'
>>> filter(lambda x: x in safe_chars, "i think i'm already doing that")
'ithinkimalreadydoingthat'

[更新]

@Daenyth I tried it, but I only get errors: from urllib import urlencode => urlencode('google.com/';) => TypeError: not a valid non-string sequence or mapping object – Marco Bruggmann

urlencode 函数旨在生成 QUERYSTRING 格式的输出 (a=1&b=2&c=3)。它需要键/值对:

>>> urllib.urlencode(dict(url='google.com/'))
'url=google.com%2F'

>>> help(urllib.urlencode)
Help on function urlencode in module urllib:

urlencode(query, doseq=0)
Encode a sequence of two-element tuples or dictionary into a URL query string.

If any values in the query arg are sequences and doseq is true, each
sequence element is converted to a separate parameter.

If the query arg is a sequence of two-element tuples, the order of the
parameters in the output will match the order of parameters in the
input.
(END)

[更新]

That will works without a doubt, but what I want is to normalize text so that I can put it on the url myself, and use it like an Id. For example, if I want to show the products of a category, I'd rather put "ninos-y-bebes" instead of "niños-y-bebés" (spanish for kids and babies). I really don't want all the áéíóúñ (which are the special characters in spanish) in my url, but I don't want to get rid of them either. That's why I would like to replace all characters that looks the same (not 100% all of them, I dont care) and then delete all non alfanumeric characters left.

好的,Marco,你想要的是创建所谓的 slugs 的例程,不是吗?

你可以一行完成:

>>> s = u'This is sóme randóm "text" that í wánt to process'
>>> allowed_chars = 'abcdefghijklmnopqrstuwvxyz01234567890'
>>> ''.join([ x if x in allowed_chars else '-' for x in unidecode(s.lower()) ])
u'this-is-some-random--text--that-i-want-to-process'
>>> s = u"Niños y Bebés"
>>> ''.join([ x if x in allowed_chars else '-' for x in unidecode(s.lower()) ])
u'ninos-y-bebes'
>>> s = u"1ª Categoria, ½ docena"
>>> ''.join([ x if x in allowed_chars else '-' for x in unidecode(s.lower()) ])
u'1a-categoria--1-2-docena'

关于Python:如何使用 string.translate() 替换引号? (用于 "slug"创建),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9623084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com