Python 中的高级 slug/url 参数化(包括音译)库

发布于 2024-12-29 02:41:11 字数 792 浏览 3 评论 0原文

我是 Python 新手,正在寻找一个 slug/url 参数化库,它提供与 Ruby Stringex 库中的功能类似的功能。例如:

# A simple prelude
"simple English".to_url => "simple-english"
"it's nothing at all".to_url => "its-nothing-at-all"
"rock & roll".to_url => "rock-and-roll"

# Let's show off
"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power"
"10% off if you act now".to_url => "10-percent-off-if-you-act-now"

# You don't even wanna trust Iconv for this next part
"kick it en Français".to_url => "kick-it-en-francais"
"rock it Español style".to_url => "rock-it-espanol-style"
"tell your readers 你好".to_url => "tell-your-readers-ni-hao"

我遇到过 webhelpers.text.urlify,它声称可以做到这一点,但是结果并不接近。非常感谢任何帮助。

I'm new to Python and am looking for a slug/url parameterization library that offers similar function to that in the Ruby Stringex library. For example:

# A simple prelude
"simple English".to_url => "simple-english"
"it's nothing at all".to_url => "its-nothing-at-all"
"rock & roll".to_url => "rock-and-roll"

# Let's show off
"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power"
"10% off if you act now".to_url => "10-percent-off-if-you-act-now"

# You don't even wanna trust Iconv for this next part
"kick it en Français".to_url => "kick-it-en-francais"
"rock it Español style".to_url => "rock-it-espanol-style"
"tell your readers 你好".to_url => "tell-your-readers-ni-hao"

I've come across webhelpers.text.urlify, which claims to do this however- the results weren't close. Any help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌无敌 2025-01-05 02:41:11

检查slugify,它基于Django 自己的 slugify 模板过滤器,但是带有 NFKD 标准化。下面是相关代码:

re.sub(r'[-\s]+', '-',
            unicode(
                re.sub(r'[^\w\s-]', '',
                    unicodedata.normalize('NFKD', string)
                    .encode('ascii', 'ignore'))
                .strip()
                .lower()))

它不像 Ruby 的 Stringex 那样强大,但您可以轻松扩展它以扩展那些与号、美元符号等。看看 UnidecodeText::Unidecode Perl 模块的 Python 端口,与 Stringex 用于 Unicode 音译的东西相同。

Check slugify, which is based on Django's own slugify template filter, but with NFKD normalization. Here's the relevant code:

re.sub(r'[-\s]+', '-',
            unicode(
                re.sub(r'[^\w\s-]', '',
                    unicodedata.normalize('NFKD', string)
                    .encode('ascii', 'ignore'))
                .strip()
                .lower()))

It's not nearly as powerful as Ruby's Stringex, but you could easily extend it to expand those ampersands, dollar symbols, etc. Take a look at Unidecode, a Python port of Text::Unidecode Perl module, the same thing Stringex uses for Unicode transliteration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文