Bonjour
le but de mon code étant de remplacer les éléments d'un fichier txt par d'autre élément du fichier csv
Je dois faire un code me permettant de parcourir les mots d'un fichier sous format .txt et de comparer ses mots avec le contenu des lignes d'un fichier csv /cellule row[0] et dans le cas ou ils sont identiques, remplacer le mot par row[2]
Le problème c'est que mon code ne passe par la boucle row in reader qu'une fois
Voici mon code
Je vous remercie
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 #!/usr/bin/python # vim: set fileencoding=utf-8 : import csv, sys, os,re #traitement fichier txt annotation=open("/home/amal/Bureau/annotation2.txt", "w") fd=open("/home/amal/Bureau/test.txt", "r") lines=[re.split("\W+", x) for x in fd.readlines()] mot=0 rows=0 line=0 #manipulation fichier csv et fichier txt fname = "tes_annotation.csv" file = open(fname, "rb") reader = csv.reader(file) for ligne in lines: #parcourir ligne par ligne le premier fichier print ("line :"+ str(line)) if (ligne[0]=="Gene"): line+=1 print ligne print "ligne commence par Gene" print ("len(ligne)) =" +str(len(ligne))) for mot in range(len(ligne)-1):#pour chauqe mot du ligne du fichier 1 #print ligne[mot] rows=0 for row in reader: #print row print ("row[0], row[2], ligne[mot] :" + " "+ row[0] + " "+ row[2] + " "+ ligne[mot]) rows+=1 print rows print "rows" if row[0]==ligne[mot]: #print "row[2]" #print row[2] #ligne[mot]=row[2] print "pas eqivalane dans la list" #ligne_transforme = ligne_transforme + ligne[mot] elif not row[0]==ligne[mot] : ###ligne_transforme = ligne_transforme + ligne[mot] print "pas eqivalane dans la list" ##annotation.write(" ".join(ligne_transforme)) ##annotation.write("\n") mot+=1 print "mot :" print mot else : print "ligne ne commence pas par Gene" print ligne ##file.close() ##fd.close() ##annotation.close()
Partager