1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| # Python 2.7
def formatx(x, codin='cp1252', codout='cp1252'):
if isinstance(x,tuple):
ch = "("
if len(x) != 0:
for elem in x:
ch += formatx(elem, codin, codout) + ", "
ch = ch[:-2] # on retire la dernière virgule
ch += ")"
elif isinstance(x,list):
ch = "["
if len(x) != 0:
for elem in x:
ch += formatx(elem, codin, codout) + ", "
ch = ch[:-2] # on retire la dernière virgule
ch += "]"
else:
# conversion de x en unicode s'il n'y est pas déjà
if not isinstance(x,unicode):
if isinstance(x,str):
x = x.decode(codin)
else:
x = u"%s" % x
# conversion si nécessaire dans l'encodage de sortie codout
if codout == 'unicode':
ch = x
else:
ch = x.encode(codout)
return ch |
Partager