# Python problème d'encodage

**URL:** <https://www.debian-fr.org/t/python-probleme-dencodage/70134>\
**Category:** Programmation\
**Created:** [Juin 26, 2016, 12:05 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134 "2016-06-26T12:05:07Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Juin 26, 2016, 12:05 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/1 "2016-06-26T12:05:07Z")

</div>

Bonjour, c’est encore moi.  
Je cherche à supprimer tous les caractères spéciaux d’une chaîne de caractère. En cherchant dans la doc j’ai construit une méthode clean.py:

```
#!/usr/bin/python
# -*- coding: utf-8 -*-
import unicodedata

def clean_string(s):
    s = unicode(s, "utf8", "replace")
    s = unicodedata.normalize('NFD', s)
    return s.encode('ascii', 'ignore')

```

ça marche très bien si je l’utilise directement par contre j’essaie de l’intégrer à mon programme:

```
#!/usr/bin/python
# -*- coding: utf-8 -*-

import clean
... 
clean.clean_string(le_nom)

```

et là j’ai une erreur:

> ```
> le_nom = unicode(le_nom, "utf8", "replace")
> TypeError: decoding Unicode is not supported
> 
> ```

Je ne comprends pas pourquoi

[edit] je précise ma variable ‹&nbsp;le\_nom&nbsp;› provient d’un widget de type entry:

`le_nom=monwidget.get()`

Merci

---

<div class="post-metadata">

**Author:** ![Almtesh](https://www.debian-fr.org/user_avatar/www.debian-fr.org/almtesh/32/10312_2.png) [@Almtesh](https://www.debian-fr.org/u/Almtesh)\
**Post date:** [Juin 26, 2016, 12:59 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/2 "2016-06-26T12:59:42Z")

</div>

Ah, encore toi.  
Quelle est la valeur de `le_nom` ?  
Peut-on avoir l’exception Python en entier ?

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Juin 26, 2016, 1:20 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/3 "2016-06-26T13:20:19Z")

</div>

Dans le\_nom il y a ce que j’entre dans le widget “éÉàÀ” par exemple

L’erreur complète est celà:

```
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
    return self.func(*args)
  File "PyRecord.py", line 87, in validNom
    clean.clean_string(le_nom)
  File "/home/PyRecord/clean.py", line 6, in clean_string
    s = unicode(s, "utf8", "replace")
TypeError: decoding Unicode is not supported
```

---

<div class="post-metadata">

**Author:** ![Almtesh](https://www.debian-fr.org/user_avatar/www.debian-fr.org/almtesh/32/10312_2.png) [@Almtesh](https://www.debian-fr.org/u/Almtesh)\
**Post date:** [Juin 26, 2016, 1:39 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/4 "2016-06-26T13:39:13Z")

</div>

La variable `le_nom` est déjà encodée en unicode, du coup, il ne peut pas le convertir.  
En fait, la fonction `unicode ()` sort déjà une variable unicode, du coup, il n’en prend pas en entrée.  
Je te conseille un truc du genre :

`def clean_string(s):
    try:
        s = unicode(s, "utf8", "replace")
    except TypeError:
        pass
    s = unicodedata.normalize('NFD', s)
    return s.encode('ascii', 'ignore')`

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Juin 26, 2016, 1:45 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/5 "2016-06-26T13:45:22Z")

</div>

> [@Almtesh](#):
>
> try: s = unicode(s, «&nbsp;utf8&nbsp;», «&nbsp;replace&nbsp;») except TypeError: pass s = unicodedata.normalize(‹&nbsp;NFD&nbsp;›, s) return s.encode(‹&nbsp;ascii&nbsp;›, ‹&nbsp;ignore&nbsp;›)

Effectivement l’erreur n’est plus là mais les accents ne sont pas supprimés ma fonction clean\_string(s) ne marche plus… Même lorsque j’essaie directement, pourtant il me semblait l’avoir vu fonctionner??

```
#!/usr/bin/python
# -*- coding: utf-8 -*-
import unicodedata

def clean_string(s):
    try:
        s = unicode(s, "utf8", "replace")
    except TypeError:
        pass
    s = unicodedata.normalize('NFD', s)
    return s.encode('ascii', 'ignore')

le_nom="éÉàÀ"
clean_string(le_nom)
print le_nom

```

ça me ressort éÉàÀ avec les accents..

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Juin 26, 2016, 1:59 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/6 "2016-06-26T13:59:30Z")

</div>

en enlevant ‹&nbsp;ignore&nbsp;› je vois l’erreur:

> ```
> return s.encode('ascii')
> 
> ```
> 
> UnicodeEncodeError: ‹&nbsp;ascii&nbsp;› codec can’t encode character u’\u0300’ in position 1: ordinal not in range(128)

---

<div class="post-metadata">

**Author:** ![Almtesh](https://www.debian-fr.org/user_avatar/www.debian-fr.org/almtesh/32/10312_2.png) [@Almtesh](https://www.debian-fr.org/u/Almtesh)\
**Post date:** [Juin 26, 2016, 2:20 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/7 "2016-06-26T14:20:39Z")

</div>

> [@totola](#):
>
> clean\_string(le\_nom)

Il faut peut-être récupérer le retour de la fonction `clean_string ()` ? Genre un truc du genre :

```auto
le_nom = clean_string(le_nom)
```

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Juin 26, 2016, 2:22 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/8 "2016-06-26T14:22:48Z")

</div>

ça ne change rien… 😕

---

<div class="post-metadata">

**Author:** ![Almtesh](https://www.debian-fr.org/user_avatar/www.debian-fr.org/almtesh/32/10312_2.png) [@Almtesh](https://www.debian-fr.org/u/Almtesh)\
**Post date:** [Juin 26, 2016, 2:24 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/9 "2016-06-26T14:24:21Z")

</div>

Ça ?

[code]#!/usr/bin/python2

# -_- coding: utf-8 -_-

import unicodedata

def clean\_string(s):  
try:  
s = unicode(s, “utf8”, “replace”)  
except TypeError:  
print (‘u’)  
pass  
s = unicodedata.normalize(‘NFD’, s)  
return s.encode(‘ascii’, ‘ignore’)

le\_nom='éÉàÀ’  
le\_nom = clean\_string(le\_nom)  
print le\_nom[/code]

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Juin 26, 2016, 2:25 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/10 "2016-06-26T14:25:13Z")

</div>

oui, c’est pareil

---

<div class="post-metadata">

**Author:** ![Almtesh](https://www.debian-fr.org/user_avatar/www.debian-fr.org/almtesh/32/10312_2.png) [@Almtesh](https://www.debian-fr.org/u/Almtesh)\
**Post date:** [Juin 26, 2016, 2:31 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/11 "2016-06-26T14:31:20Z")

</div>

Ben, ça alors, moi, j’ai ce code-là :

[code]#!/usr/bin/python2

# -_- coding: utf-8 -_-

import unicodedata

def clean\_string(s):  
try:  
s = unicode(s, “utf8”, “replace”)  
except TypeError:  
pass  
s = unicodedata.normalize(‘NFD’, s)  
return s.encode(‘ascii’, ‘ignore’)

le\_nom='éÉàÀ’  
le\_nom = clean\_string(le\_nom)  
print le\_nom[/code]  
et ça me retourne ça quand je le lance :

`┌ (gilles@Stardust + 0) (26/06/16 - 16:23:33) (0.95 - 18%) (~)
└% python2 ./misc/le_nom.py
eEaA`

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Juin 26, 2016, 2:36 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/12 "2016-06-26T14:36:38Z")

</div>

Ah moi ça marche maintenant!!  
Je sais plus ce que j’ai changé mais le code que tu as mis juste à l’instant marche très bien chez moi  
Ça me sort bien:  
eEaA

---

<div class="post-metadata">

**Author:** ![Almtesh](https://www.debian-fr.org/user_avatar/www.debian-fr.org/almtesh/32/10312_2.png) [@Almtesh](https://www.debian-fr.org/u/Almtesh)\
**Post date:** [Juin 26, 2016, 2:38 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/13 "2016-06-26T14:38:33Z")

</div>

> [@totola](#):
>
> Ah moi ça marche maintenant!!

Super, je me disais bien.  
Bon, problème résolu.

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Juin 26, 2016, 2:39 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/14 "2016-06-26T14:39:52Z")

</div>

Oui, merci!  
Jusqu’au prochain… 😉

---

<div class="post-metadata">

**Author:** ![totola](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/3be4f8/32.png) [@totola](https://www.debian-fr.org/u/totola)\
**Post date:** [Juin 26, 2016, 2:41 UTC](https://www.debian-fr.org/t/python-probleme-dencodage/70134/15 "2016-06-26T14:41:29Z")

</div>

Mais l’autre ne l’est toujours pas… Bon pour l’instant ce n’est pas grave je n’ai qu’une vingtaine de commande à écrire à la main, mais je sais que j’ai une fonction où il me faudra écrire 150 commandes selon … 😕
