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
| #!/usr/bin/env python
#coding=utf-8
import decimal
f_entree = open( "fichier.txt", "r")
f_sortie = open( "fichier_sortie.txt", "w")
f_sortie_de_nombres = open( "fichier_sortie_de_nombres.txt", "w")
for ligne in f_entree:
# ligne=" 11.00 12.140560\n"
ligne_textes=ligne.split()
# ligne_textes=['11.00', '12.140560']
f_sortie.write(ligne_textes[0]+","+ligne_textes[1]+"\n") # ==> Création du fichier à partir du texte
if ligne_textes==['&', 'titre']:
print "nouveau titre"
else:
ligne_nombres=[decimal.Decimal(t) for t in ligne_textes]
# Transformation en decimal pour maitriser les arrondis
# Exemple ligne_nombres=[Decimal("11.00"), Decimal("12.140560")]
ligne_nombres[0]+=100 # Calcul sur les valeurs
f_sortie_de_nombres.write(str(ligne_nombres[0])+","+str(ligne_nombres[1])+"\n")
# ==> Création du fichier à partir de nombre calculés
f_entree.close()
f_sortie.close()
f_sortie_de_nombres.close() |
Partager