Précision conversion atof()

Précision conversion atof() - Python - Programmation

Marsh Posté le 23-03-2009 à 20:43:16    

Bonsoir,
sous Windows XP, Python 2.5, j'importe un fichier data.csv via csv reader.
Je veux convertir les string contenus dans le .csv en format numérique.
Par ex :
 
for i in range(1,nb_lg):
        for j in range(0,nb_col):
                item=data[i][j]
                if item is not '':
                    item=item.replace(',','.')
                    data[i][j]=string.atof(item)
 
Mais pour certaines valeurs, par ex. item='0,95', ça me le transforme en item=0.95000000001 ou item=0.94999999999 !
Manque de Précision ?
Comment toujours obtenir item=0.95 ?
 
Merci pour vos réponses !
 :jap:

Reply

Marsh Posté le 23-03-2009 à 20:43:16   

Reply

Marsh Posté le 23-03-2009 à 21:33:12    

gennosuke a écrit :

Mais pour certaines valeurs, par ex. item='0,95', ça me le transforme en item=0.95000000001 ou item=0.94999999999 !


C'est parfaitement normal. Accessoirement atof n'a aucun intérêt:

Code :
  1. >>> import string
  2. >>> string.atof('0.95')
  3. 0.94999999999999996
  4. >>> float('0.95')
  5. 0.94999999999999996
  6. >>>


gennosuke a écrit :

Manque de Précision ?


Pas du tout

gennosuke a écrit :

Comment toujours obtenir item=0.95 ?


http://docs.python.org/library/decimal.html

 

Accessoirement,

 
  • range(0,nb_col) => range(nb_col)
  • if item is not '': => if item


Message édité par masklinn le 23-03-2009 à 21:35:54

---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 23-03-2009 à 22:08:10    

et surtout: http://docs.python.org/library/csv.html


---------------
Nos estans firs di nosse pitite patreye...
Reply

Marsh Posté le 23-03-2009 à 22:14:15    

OK, merci !
Alors :
>>> data[1]
['1', '85', '44729', '42', '458', '3760011174639', '1074416', '6,45', '1']
>>> num_data(data) #avec atof()
>>> data[1]
[1.0, 85.0, 44729.0, 42.0, 458.0, '3760011174639', 1074416.0, 6.4500000000000002, 1.0]
 
tandis que:
>>> data[1][7]=data[1][7].replace(',','.')
>>> data[1][7]
'6.45'
>>> print Decimal(data[1][7])
6.45
 
Ca marche, mais je souhaite :
data[1][7] <- Decimal(data[1][7])
ce qui ferait data[1][7]=6.45
sans passer par print.
 
Je n'ai pas d'idée !

Reply

Marsh Posté le 23-03-2009 à 22:14:27    


Citation :

j'importe un fichier data.csv via csv reader.


:o

gennosuke a écrit :

OK, merci !
Alors :

Code :
  1. >>> data[1]
  2. ['1', '85', '44729', '42', '458', '3760011174639', '1074416', '6,45', '1']
  3. >>> num_data(data) #avec atof()
  4. >>> data[1]
  5. [1.0, 85.0, 44729.0, 42.0, 458.0, '3760011174639', 1074416.0, 6.4500000000000002, 1.0]
 

tandis que:

Code :
  1. >>> data[1][7]=data[1][7].replace(',','.')
  2. >>> data[1][7]
  3. '6.45'
  4. >>> print Decimal(data[1][7])
  5. 6.45
 

Ca marche, mais je souhaite :
data[1][7] <- Decimal(data[1][7])
ce qui ferait data[1][7]=6.45
sans passer par print.

 

Je n'ai pas d'idée !


T'as réfléchi 3s et essayé de remplacer ton atof pourri par un appel à Decimal, dans ta fonction?

Message cité 1 fois
Message édité par masklinn le 23-03-2009 à 22:15:55

---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 23-03-2009 à 22:15:10    

masklinn a écrit :


Citation :

j'importe un fichier data.csv via csv reader.


:o


alors l'utilisation de range est foireuse...


---------------
Nos estans firs di nosse pitite patreye...
Reply

Marsh Posté le 23-03-2009 à 22:17:31    

KangOl a écrit :


alors l'utilisation de range est foireuse...


Bah non.


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 23-03-2009 à 22:43:43    

OK, c'est bon.
Après un temps de réflexion supérieur à 3s, j'ai remplacé atof par Decimal.
On trimballe des Decimal(".." ) partout, mais la variable est changée.
Merci

Reply

Marsh Posté le 24-03-2009 à 06:46:18    

Je vous aime bien avec le Decimal, mais on dirait un cours de maths de première année de fac: la majorité des étudiants est incapable d'admettre que 0.99999... = 1. C'est à dire qu'un nombre n'a pas une représentation unique. (Et vice versa en float ;).
 
Au final, sauf calcul financier, rester en float convient très bien. Si les ....02 dérange quelqu'un, y a qu'à formater la sortie et on en parle plus.

Reply

Marsh Posté le 24-03-2009 à 10:05:12    

OK, merci des conseils!
J'en ferai bon usage.
A +

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed