From 5b423265064165c1bc683d7464739a63447efc56 Mon Sep 17 00:00:00 2001 From: gwen Date: Wed, 13 May 2015 15:37:00 +0200 Subject: [PATCH] corrections coquilles --- python/formation/specialmethods.txt | 2 +- python/formation/type.txt | 150 ++++++++++++++++++++-------- 2 files changed, 108 insertions(+), 44 deletions(-) diff --git a/python/formation/specialmethods.txt b/python/formation/specialmethods.txt index a80616b..2172ae9 100644 --- a/python/formation/specialmethods.txt +++ b/python/formation/specialmethods.txt @@ -61,7 +61,7 @@ Organisation modulaire modules chargés et modules importés -------------------------------------- -Les modules susceptibles d'être chargés sont dans le :envvar:`PYTHNONPATH`. +Les modules susceptibles d'être chargés sont dans le :envvar:`PYTHONPATH`. Mais comment peut-on savoir ou ils sont physiquement (sur le disque dur) ? .. envvar:: `sys.modules` diff --git a/python/formation/type.txt b/python/formation/type.txt index 9c84e31..93c21fc 100644 --- a/python/formation/type.txt +++ b/python/formation/type.txt @@ -118,6 +118,66 @@ index(), find(), replace() ['df', 'df', 'df', 'df', 'df'] >>> +unicode +------------ + +En python 2.X : deux types : `str` et `unicode` (en python 3 ces types sont unifiés) + +on peut facilement tomber sur des erreurs unicode:: + + UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: + ordinal not in range(128) + + +- l'encodage (unicode): + +on part d'un objet unicode : + +>>> u = u"éèà bla" +>>> u +u'\xe9\xe8\xe0 bla' + +on le transforme en string utf-8 : + +>>> u.encode("utf-8") +'\xc3\xa9\xc3\xa8\xc3\xa0 bla' +>>> print u.encode("utf-8") +éèà bla +>>> + +on peut partir d'une string en utf-8, puis:: + + + +manips importantes de traitement unicode (si on n'est pas en python 3) + +>>> u = u"ésdsfè" +>>> u +u'\xe9sdsf\xe8' +>>> print u +ésdsfè +>>> str(u) +Traceback (most recent call last): + File "", line 1, in +UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: +ordinal not in range(128) +>>> u.encode("utf-8") +'\xc3\xa9sdsf\xc3\xa8' +>>> s = u.encode("utf-8") +>>> type(s) + +>>> + +Il faut utiliser ``.encode()``, et pas ``.decode()``:: + + if type(s) == unicode #types.UnicodeType: + bla bla + + if type(s) == str: + rien à faire + +manipulations diverses : + - enlever les accents >>> import unicodedata @@ -136,52 +196,11 @@ u'un ete meme pas chaud' >>> string.punctuation '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' -- l'encodage (unicode): ->>> u = u"éèà bla" ->>> u -u'\xe9\xe8\xe0 bla' ->>> u.encode("utf-8") -'\xc3\xa9\xc3\xa8\xc3\xa0 bla' ->>> print u.encode("utf-8") -éèà bla ->>> -manips importantes de traitement unicode (si on n'est pas en python 3) +tuples, listes, dictionnaires +--------------------------------- ->>> u = u"ésdsfè" ->>> u -u'\xe9sdsf\xe8' ->>> print u -ésdsfè ->>> str(u) -Traceback (most recent call last): - File "", line 1, in -UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: -ordinal not in range(128) ->>> u.decode("utf-8") -Traceback (most recent call last): - File "", line 1, in - File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode - return codecs.utf_8_decode(input, errors, True) -UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: -ordinal not in range(128) ->>> u.encode("utf-8") -'\xc3\xa9sdsf\xc3\xa8' ->>> s = u.encode("utf-8") ->>> type(s) - ->>> - -Il faut utiliser ``.encode()``, et pas ``.decode()``:: - - if type(s) == unicode #types.UnicodeType: - bla bla - - if type(s) == str: - rien à faire - -- tuples, listes, dictionnaires >>> t = (1,2,3) >>> l = [1,2,3] @@ -199,7 +218,52 @@ Il faut utiliser ``.encode()``, et pas ``.decode()``:: >>> l ['e', 'q'] >>> +exercice +------------- +écrire la string "1-2-3-4-5-6-7-8-9" programmatiquement + +>>> [str(i) for i in l] +['1', '2', '3', '4', '5', '6', '7', '8', '9'] +>>> l2 = [] +>>> for i in l: +... l2.append(str(i)) +... +>>> l2 +['1', '2', '3', '4', '5', '6', '7', '8', '9'] +>>> +>>> l = range(1,9) +>>> l2 = [str(i) for i in l] +>>> "-".join(l2) +'1-2-3-4-5-6-7-8' +>>> s= "-" +>>> l2.extend(range(20)) +>>> l2 +['1', '2', 'sdfsdf', '3', '4', '5', '6', '7', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] +>>> l + l2 +[1, 2, 3, 4, 5, 6, 7, 8, '1', '2', 'sdfsdf', '3', '4', '5', '6', '7', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] +>>> l.extend(l2) +KeyboardInterrupt +>>> l = [] +>>> l = list() +>>> list + +>>> list() +[] +>>> list(range(2)) +[0, 1] +>>> tuple + +>>> t = (1,2,3) +>>> t +(1, 2, 3) +>>> list(t) +[1, 2, 3] +>>> t +(1, 2, 3) +>>> type(t) + +>>> .. important:: utiliser get plutôt que l'accès par items lorsque l'on n'est pas sûr