I want to replace a part of text in a file text with python. My part of text to add is stored with a list as you can see with "a list". I detect the part of the text with a counter that works well, my problem seems to be due to my part of ".write" which is defined in a loop with : "fichier_write.write("%.4\n" %a[:][i]) "
#The list I have to add in my file text
a = ['"A01.tiff", "B01.tiff"',
'"A02.tiff","B02.tiff"',
'"A03.tiff","B03.tiff"',
'"A04.tiff","B04.tiff"']
#The format expected is : (i have to suppress " ' " and " [ ] "
"A01.tiff", "B01.tiff",
"A02.tiff","B02.tiff",
"A03.tiff","B03.tiff",
"A04.tiff","B04.tiff"
fichier = open('original.txt',"r")
nb_ligne = 0
chaine = 'image_list ='
for ligne in fichier:
nb_ligne += 1
if chaine in ligne:
break
fichier.close()
#I try to modify my file text with :
fichier_read = open('original.txt',"r")
fichier_write = open('modified.txt',"w")
compteur=0.
for ligne in fichier_read:
compteur=compteur+1
if compteur != nb_ligne :
fichier_write.write(ligne)
else :
fichier_write.write('image_list = \n')
for i in range(len(a)):
fichier_write.write("%.4\n" %a[:][i])
break
fichier_write.close()
fichier_read.close()