corrections coquilles
This commit is contained in:
parent
7ad34d4962
commit
1e21dbe0f9
|
@ -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`
|
||||
|
|
|
@ -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 "<stdin>", line 1, in <module>
|
||||
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)
|
||||
<type 'str'>
|
||||
>>>
|
||||
|
||||
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 "<stdin>", line 1, in <module>
|
||||
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 "<stdin>", line 1, in <module>
|
||||
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)
|
||||
<type 'str'>
|
||||
>>>
|
||||
|
||||
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
|
||||
<type 'list'>
|
||||
>>> list()
|
||||
[]
|
||||
>>> list(range(2))
|
||||
[0, 1]
|
||||
>>> tuple
|
||||
<type 'tuple'>
|
||||
>>> t = (1,2,3)
|
||||
>>> t
|
||||
(1, 2, 3)
|
||||
>>> list(t)
|
||||
[1, 2, 3]
|
||||
>>> t
|
||||
(1, 2, 3)
|
||||
>>> type(t)
|
||||
<type 'tuple'>
|
||||
>>>
|
||||
|
||||
|
||||
.. important:: utiliser get plutôt que l'accès par items lorsque l'on n'est pas sûr
|
||||
|
|
Loading…
Reference in New Issue