Регулярно требуется преобразовать какой-либо текст в максимально совместимый текст для URL, имени файла, имени объекта в каком-то софте и тд. Требования совместимости простые: в тексте должны быть только допустимые символы. Обычно это a-z, 0-9 и "_" или "-". То есть, только прописные буквы латинского алфавита и цифры (как пример).
Допустим, нам нужно название статьи в блоге преобразовать в slug для добавления его в URL этой статьи. Как это лучше всего сделать?
В Django по умолчанию есть готовая функция slugify для таких случаев.
Но я её никогда не использую. Почему? Потому что её недостаточно!
Приведём пример
>>> from django.utils.text import slugify
>>> slugify('This is a Title')
'this-is-a-title'
Пока всё отлично
>>> slugify('This is a "Title!"')
'this-is-a-title'
Спец символы удалились, всё хорошо.
>>> slugify('Это заголовок статьи')
''
Вот и приехали 😢. Если текст не английский то буквы просто игнорируются. Можно это поправить
>>> slugify('Это заголовок статьи', allow_unicode=True)
'это-заголовок-статьи'
Но тогда мы не вписываемся в условие. У нас появилась кириллица в тексте.
Так как я часто пишу сайты для русскоязычных пользователей эта проблема весьма актуальна. Я не использую стандартную функцию и всегда пишу свою.
Оригинал я не беру в расчёт и пишу полностью свою функцию. И так, по порядку:
🔸1. Исходный текст:
>>> text = 'Мой заголовок №10 😁!'
Взял специально посложней со специальными символами.
🔸2. Транслит
Необходимо сделать транслит всех символов в латиницу. Здесь очень выручает библиотека unidecode. Помимо простого транслита кириллицы в латиницу она умеет преобразовывать спец символы и иероглифы в текстовые аналоги.
from unidecode import unidecode
>>> unidecode("Ñ Σ ® µ ¶ ¼ 月 山")
'N S (r) u P 1/4 Yue Shan'
Очень крутая библиотека, советую👍
В нашем случае получаем такое преобразование:
>>> text = unidecode(text)
>>> print(text)
'Moi zagolovok No. 10 !'
Отличный транслит. Смайл просто удалился, хотя я ждал что-то вроде :). Ну и ладно, всë равно невалидные символы.
А еще наш код уже поддерживает любой язык, будь то хинди или корейский.
🔸4. Фильтр символов
Unidecode не занимается фильтрацией по недопустимым символам. Это мы делаем в следующем шаге через regex. Просто заменим все символы на "_" если они вне указанного диапазона.
>>> text = re.sub(r'[^a-zA-Z0-9]+', '_', text)
>>> print(text)
'Moi_zagolovok_No_10_'
Символ "+" в паттерне выручает когда несколько недопустимых символов идут рядом. Все они заменяются на один символ "_".
🔸5. Slugify
Осталось удалить лишние символы по краям и сделать нижний регистр
>>> text = text.strip('_').lower()
>>> print(text)
'moi_zagolovok_no_10'
Получаем отличный slug! 😎
🌎 Полный код в виде функции.
______________
PS. Проверку что в строке остался хоть один допустимый символ я бы вынес в отдельную функцию.
#libs#tricks#django
#DidYouKnow❓
Relying on the healing power of nature, Naturopathy stimulates the human body's ability to heal itself!
Test your knowledge about Naturopathy. Take the quick quiz today!
Visit: https://quiz.mygov.in/quiz/naturopathy-quiz/
📢 The Weekly Wrap – 16 May 2025
1) #DidYouKnow Today is Drug Victims Remembrance Day?
An annual observance to rally together on the harms and trauma of drug abuse.
Visit the “Museum of Us” exhibition from 16 to 18 May at Suntec City, 10am to 8pm, to get an insider perspective on the impact of drug abuse and offer support in the healing process.
🔗 Show your support at: go.gov.sg/rday2025.
2) The Together, for Better roadshows: Back in your ‘hood!
Our roadshows are back, and we’re making our way to Bukit Panjang and Hougang this weekend!
What’s in store for you:
✅ Interactive exhibits
💼 Professional advice on jobs and upskilling
🎁 Join quizzes and win prizes
🔗 More details: togetherforbetter.gov.sg/events-and-roadshows/
See you at Hillion Mall and Hougang Mall on 17 & 18 May, from 11am to 8pm!
Daily Questions and Answers
1. The ‘Roof’ of the world is referred to which place?
Answer: Pamir knot
2. What is the currency in Cambodia?
Answer: Riel
3. What is dew point?
Answer: The temperature at which dew begins to evaporate
4. How do you measure the speed of wind?
Answer: Anemometer
5. Name the river that is not crossed by any bridges.
Answer: Amazon
#DidYouKnow
One million Earths could fit inside the sun!
Via @askmenow
News headlines: read here
Try: Quiz MCQ
For Fun: Be Like Bro , sarcasticLOL
1. How many countries are present in Africa?
Answer: 54 countries
2. Which planet is closest to Planet Earth?
Answer: Venus
3. What erupts from a volcano?
Answer: Lava, steam, ash, pieces of rock, and gaseous sulfur compounds
4. What is the name of the strait that separates the north and south islands of New Zealand?
Answer: Cook strait
5. Which is the direction at which wind blows?
Answer: Leeward direction
#DidYouKnow
At over 2,000 kilometers long, The Great Barrier Reef is the largest living structure on Earth
Via @askmenow
Know more about:
1- Venezuela Oil Crisis
2. Rohingya Refugee Crisis
News headlines(29.01.2021): read here
Try: Quiz MCQ
For Fun:Be Like Bro , sarcasticLOL
Today's Questions and Answers
1. Which is the coldest continent in the world?
Answer: Antarctica
2. Which is the world’s smallest country?
Answer: Vatican City
3. Where is the Colloseum located?
Answer: Rome, Italy
4. Where is the Tower Bridge located?
Answer: London, UK
5. Where is the Leaning Tower of Pisa located?
Answer: Pisa, Italy
#DidYouKnow
There is enough DNA in the average person’s body to stretch from the sun to Pluto and back — 17 times.
Via @askmenow
Today's news headlines: read here
Try: Quiz MCQ
For Fun: Be Like Bro , sarcasticLOL
Today's Questions and Answers
1. Which is the largest waterfall in the world?
Answer: Victoria Falls
2. Which is the largest desert in the world?
Answer: Sahara Desert
3. What are the branches of a river called?
Answer: Tributaries
4. Which is the largest island in the world?
Answer: Greenland
5. What heats the water in a hot spring?
Answer: Magma
#DidYouKnow
In a glass of water when you pour a handful of salt, the level of water does down
Via @askmenow
Today's news headlines: read here
Try: Quiz MCQ
For Fun: Be Like Bro , sarcasticLOL
Today's Questions and Answers
1. Which is the tallest mountain in the world?
Answer: Mount Everest
2. Which is the latitude that runs through the centre of the Earth?
Answer: Equator
3. What are the names of the 5 oceans of the world?
Answer: Indian Ocean, Southern Ocean, Atlantic Ocean, Pacific Ocean, and Arctic Ocean
4. Which country has the highest population?
Answer: China
5. Which is the longest river in the world?
Answer: The Nile, Africa
#DidYouKnow
The Paris Agreement on climate change was signed by the largest number of countries ever in one day.
Via @askmenow
Today's news headlines: read here
For fun visit : Be Like Bro , sarcasticLOL
Try : Quiz MCQ
Daily Questions and Answers
1. Aurora Borealis are created in which layer of atmosphere?
Ans. Thermosphere
2. What is the instrument used to measure wind speed?
Ans. Anemometer
3. Eiffel Tower is six inches taller in summer due to-
Ans. Thermal Expansion
4. Eiffel Tower is taller in which season-
Ans. Summer Season
5. Who was the first person to win the Nobel prize in physics?
Ans. Wilhelm Conrad Röntgen (1901)
#DidYouKnow
The reason the ocean has waves and has tides that switch from high to low is that the gravitational pull from the moon and the sun keep changing.
Via @askmenow
Know more about:
1- Aurora Borealis vs Aurora Australis
Today's headlines: read here
For fun: Visit @sarcasticLOL & @updateee
Daily Questions and Answers
1. Sleeping under the tree at night time is dangerous because trees produce some gas, what is the gas?
Ans. Carbon Dioxide
2. Sound waves cannot travel in?
Ans. Space or Vacuum
3. What is the full form of NASA?
Ans. National Aeronautics and Space Administration
4. What is the atomic number of sodium?
Ans. 11
5. What are the three subatomic particles in an atom?
Ans. Eelectrons, protons and neutrons
#DidYouKnow
The decimal number system we use today was invented in India.
Via @askmenow
Latest news: Hong Kong internet provider admits blocking pro-democracy website
Have fun with @sarcasticLOL
Daily Questions and Answers
1. We live in which layer of atmosphere?
Ans. Troposphere
2. 1 Nanometer is equals to how many meters?
Ans. 10 power minus 9 meters
3. Sun appears red at morning and evening because of?
Ans. Scattering of light
4. Which Scientist named the words positive and negative in electrical charges?
Ans. Benjamin Franklin
5. A Reusable space rocket is called as?
Ans. Space shuttle
#DidYouKnow
Dogs have an amazing sense of hearing and can hear sounds at four times the distance than humans can.
Via @askmenow
Latest news:Yellow mealworms become first insect approved in EU as human food
Know more about:Mealworm