]> gitweb.pimeys.fr Git - scripts-20-100.git/commitdiff
useful python trick
authorVincent Le Gallic <legallic@crans.org>
Sun, 29 Sep 2013 19:23:53 +0000 (21:23 +0200)
committerVincent Le Gallic <legallic@crans.org>
Sun, 29 Sep 2013 19:23:53 +0000 (21:23 +0200)
remplace_accents.py [new file with mode: 0644]

diff --git a/remplace_accents.py b/remplace_accents.py
new file mode 100644 (file)
index 0000000..687b750
--- /dev/null
@@ -0,0 +1,27 @@
+#!/usr/bin/python
+# -*- coding:utf8 -*-
+
+""" Tout petit script à importé partout où on en a besoin
+    pour enlever les accents/œ/æ/trémas d'une chaîne unicode
+"""
+
+def remplace_accents(chaine):
+    remplacements = {
+                u"’":u"'",
+                u"á":u"a", u"à":u"a", u"â":u"a", u"ä":u"a",
+                u"Á":u"A", u"À":u"A", u"Â":u"A", u"Ä":u"A",
+                u"é":u"e", u"è":u"e", u"ê":u"e", u"ë":u"e",
+                u"É":u"E", u"È":u"E", u"Ê":u"E", u"Ë":u"E",
+                u"í":u"i", u"ì":u"i", u"î":u"i", u"ï":u"i", 
+                u"Í":u"I", u"Ì":u"I", u"Î":u"I", u"Ï":u"I", 
+                u"ó":u"o", u"ò":u"o", u"ô":u"o", u"ö":u"o",
+                u"Ó":u"O", u"Ò":u"O", u"Ô":u"O", u"Ö":u"O",
+                u"ú":u"u", u"ù":u"u", u"û":u"u", u"ü":u"u",
+                u"Ú":u"U", u"Ù":u"U", u"Û":u"U", u"Ü":u"U",
+                u"ý":u"y", u"ỳ":u"y", u"ŷ":u"y", u"ÿ":u"y",
+                u"Ý":u"Y", u"Ỳ":u"Y", u"Ŷ":u"Y", u"Ÿ":u"Y",
+                u"œ":u"oe", u"æ":u"ae",
+                u"Œ":u"OE", u"Æ":u"AE"}
+    for avant,apres in remplacements.items():
+        chaine=chaine.replace(avant,apres)
+    return chaine