Supprimer des accents d'un texte, rien de plus simple
#DynamoBIM #Python #RevitAPI #Unicode #AutodeskExpertElite #AutodeskCommunity 
if this article is not in your language, use the Google Translate
widget ⬈ (bottom of page for Mobile version ⬇)
Si votre but est de transformer
Éléphant en
Elephant, voici une méthode
utilisant la Normalisation Unicode.
- D'abord on normalise la chaîne Unicode vers une décomposition NFD.
(forme composée NFC vers une forme décomposée NFD)
Cette opération sépare la lettre de son accent ( é devient e + ´ ). - Puis on parcours le texte et retire tout ce qui appartient à la catégorie Mn (Mark, Nonspacing), c'est-à-dire les accents.
Dans l'univers Unicode, ces accents isolés ('̈', '̊', '̂', '́', '̌') appartiennent tous à la catégorie "Mn" (Mark, Nonspacing).
Les lettres normales ('H', 'e', 'l') appartiennent aux catégories "Lu" (Majuscule) ou "Ll" (Minuscule).
unicodedata
import sys
import unicodedata
def altClean(txt):
if not isinstance(txt, str): return txt
forme_nfd = unicodedata.normalize('NFD', txt)
# tip tp Visualise all the characters as separate.
print(list(forme_nfd))
return "".join(c for c in forme_nfd if unicodedata.category(c) != 'Mn')
OUT = altClean(IN[0])
méthode avec la classe
System.String(.Net)
import clr
import System
from System.Text import StringBuilder, NormalizationForm
from System.Globalization import UnicodeCategory, CharUnicodeInfo
def altClean(text):
if not text: return System.String.Empty
#
normalizedString = System.String(text).Normalize(NormalizationForm.FormD)
# tip tp Visualise all the characters as separate.
print(list(normalizedString))
sb = StringBuilder()
for c in normalizedString:
if CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark:
sb.Append(c)
return System.String(sb.ToString()).Normalize(NormalizationForm.FormC)
OUT = altClean(IN[0])
..
"Il y a deux façons de se tromper : L'une est de croire ce qui n'est pas, l'autre de refuser de croire ce qui est."
Sören Kierkegaard




0 commentaires:
Enregistrer un commentaire