Python 中的高级 slug/url 参数化(包括音译)库
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查slugify,它基于Django 自己的 slugify 模板过滤器,但是带有 NFKD 标准化。下面是相关代码:
它不像 Ruby 的 Stringex 那样强大,但您可以轻松扩展它以扩展那些与号、美元符号等。看看 Unidecode,
Text::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:
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.