本文主要是介绍Python100个库分享第13个—awesome-slugify(处理Unicode),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 专栏导读
- 库的介绍
- 库的安装
- 基础用法1:用‘-’连接
- 基础用法1:汉字转拼音用‘-’连接
- 有个类似的库 —python-slugify
- 安装
- 总结
专栏导读
🌸 欢迎来到Python办公自动化专栏—Python处理办公问题,解放您的双手
🏳️🌈 博客主页:请点击——> 一晌小贪欢的博客主页求关注
👍 该系列文章专栏:请点击——>Python办公自动化专栏求订阅
🕷 此外还有爬虫专栏:请点击——>Python爬虫基础专栏求订阅
📕 此外还有python基础专栏:请点击——>Python基础学习专栏求订阅
文章作者技术和水平有限,如果文中出现错误,希望大家能指正🙏
❤️ 欢迎各位佬关注! ❤️
库的介绍
-
awesome-slugify是一个Python slugify库,主要用于处理Unicode。
-
slugify是一种常见的字符串处理方式,用于将字符串转换为适合URL路径的格式,通常是将字符串中的空格替换为连字符(-)或下划线(_),并删除特殊字符。
-
在处理包含Unicode字符的字符串时,awesome-slugify库可以很好地处理这些字符,将它们转换为适合URL的格式。
库的安装
pip install awesome-slugify -i https://pypi.tuna.tsinghua.edu.cn/simple/
基础用法1:用‘-’连接
from slugify import slugify
res = slugify ( 'Any text' )
print(res)
-
输出
Any-text
基础用法1:汉字转拼音用‘-’连接
from slugify import slugify
title = "我的第一篇博客文章:关于Python编程"
slug = slugify(title)
print(slug)
-
输出
Wo-De-Di-Yi-Pian-Bo-Ke-Wen-Zhang-Guan-Yu-PythonBian-Cheng
有个类似的库 —python-slugify
安装
pip install python-slugify -i https://pypi.tuna.tsinghua.edu.cn/simple/
from slugify import slugifytxt = "This is a test ---"
r = slugify(txt)
self.assertEqual(r, "this-is-a-test")txt = "___This is a test ---"
r = slugify(txt)
self.assertEqual(r, "this-is-a-test")txt = "___This is a test___"
r = slugify(txt)
self.assertEqual(r, "this-is-a-test")txt = "This -- is a ## test ---"
r = slugify(txt)
self.assertEqual(r, "this-is-a-test")txt = '影師嗎'
r = slugify(txt)
self.assertEqual(r, "ying-shi-ma")txt = 'C\'est déjà l\'été.'
r = slugify(txt)
self.assertEqual(r, "c-est-deja-l-ete")txt = 'Nín hǎo. Wǒ shì zhōng guó rén'
r = slugify(txt)
self.assertEqual(r, "nin-hao-wo-shi-zhong-guo-ren")txt = 'jaja---lol-méméméoo--a'
r = slugify(txt)
self.assertEqual(r, "jaja-lol-mememeoo-a")txt = 'Компьютер'
r = slugify(txt)
self.assertEqual(r, "kompiuter")txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=9)
self.assertEqual(r, "jaja-lol")txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=15)
self.assertEqual(r, "jaja-lol-mememe")txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=50)
self.assertEqual(r, "jaja-lol-mememeoo-a")txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=15, word_boundary=True)
self.assertEqual(r, "jaja-lol-a")txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=17, word_boundary=True)
self.assertEqual(r, "jaja-lol-mememeoo")txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=18, word_boundary=True)
self.assertEqual(r, "jaja-lol-mememeoo")txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=19, word_boundary=True)
self.assertEqual(r, "jaja-lol-mememeoo-a")txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=20, word_boundary=True, separator=".")
self.assertEqual(r, "jaja.lol.mememeoo.a")txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=20, word_boundary=True, separator="ZZZZZZ")
self.assertEqual(r, "jajaZZZZZZlolZZZZZZmememeooZZZZZZa")txt = 'one two three four five'
r = slugify(txt, max_length=13, word_boundary=True, save_order=True)
self.assertEqual(r, "one-two-three")txt = 'one two three four five'
r = slugify(txt, max_length=13, word_boundary=True, save_order=False)
self.assertEqual(r, "one-two-three")txt = 'one two three four five'
r = slugify(txt, max_length=12, word_boundary=True, save_order=False)
self.assertEqual(r, "one-two-four")txt = 'one two three four five'
r = slugify(txt, max_length=12, word_boundary=True, save_order=True)
self.assertEqual(r, "one-two")txt = 'this has a stopword'
r = slugify(txt, stopwords=['stopword'])
self.assertEqual(r, 'this-has-a')txt = 'the quick brown fox jumps over the lazy dog'
r = slugify(txt, stopwords=['the'])
self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')txt = 'Foo A FOO B foo C'
r = slugify(txt, stopwords=['foo'])
self.assertEqual(r, 'a-b-c')txt = 'Foo A FOO B foo C'
r = slugify(txt, stopwords=['FOO'])
self.assertEqual(r, 'a-b-c')txt = 'the quick brown fox jumps over the lazy dog in a hurry'
r = slugify(txt, stopwords=['the', 'in', 'a', 'hurry'])
self.assertEqual(r, 'quick-brown-fox-jumps-over-lazy-dog')txt = 'foo & bar'
r = slugify(txt)
self.assertEqual(r, 'foo-bar')
总结
-
希望对初学者有帮助
-
致力于办公自动化的小小程序员一枚
-
希望能得到大家的【一个免费关注】!感谢
-
求个 🤞 关注 🤞
-
此外还有办公自动化专栏,欢迎大家订阅:Python办公自动化专栏
-
求个 ❤️ 喜欢 ❤️
-
此外还有爬虫专栏,欢迎大家订阅:Python爬虫基础专栏
-
求个 👍 收藏 👍
-
此外还有Python基础专栏,欢迎大家订阅:Python基础学习专栏
这篇关于Python100个库分享第13个—awesome-slugify(处理Unicode)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!