Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
data iteration in a file
#1
Hello
I would like to know if it is possible to iterate the strings to be entered in a file

Normally, as in the example below, we enter a character string after the write operator ( ) or after line=

fich = open("mon_fichier.txt", "w")
fich.write("Hello world")
line = "Hello".
fich.write(line)
file.close()
Now I would like to place in write( ) or line = , strings resulting from a previous script including loops that allow me to generate these different strings

Thank you for your help
----------------------------
Bonjour

je voudrais savoir s’il est possible d’itérer les chaînes de caractères à saisir dans un fichier

Normalement ,comme dans l’exemple ci dessous , on saisit une caine de caractère après l’opérateur write ( ) ou après ligne=

fich = open("mon_fichier.txt","w")
fich.write("Bonjour le monde\n")
ligne = "Coucou\n"
fich.write(ligne)
fich.close()
Or je souhaiterai placer dans write( ) ou ligne = , des chaînes de caractère résultant d’un script précédent comprenant des boucles qui me permettent de générer ces différentes chaînes

Merci de votre aide
cordialement
cordially


Translated with www.DeepL.com/Translator (free version)
buran write Mar-17-2021, 07:34 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Perhaps this will put you in the right direction.
Peut-être que cela vous mettra dans la bonne direction.

def obtenir_des_commentaires () :
	print ('\nEntrez un texte à ajouter au fichier.')
	return input ('Appuyez sur entrez uniquement pour terminer l’entrée : ')

def écrire_des_choses_à_déposer () :
	fich = open ('mon_fichier.txt', 'w')
	while True :
		line = obtenir_des_commentaires () 
		if line != '' :
			fich.write (line + '\n')
		else :
			fich.close ()
			print ('\nTout est fait.')
			break

écrire_des_choses_à_déposer ()
Reply
#3
ok I will be careful with the BBcodes!
thanks for your answer ....
i inserted it in my own script and it works except that i have to do "enter" to acquire each line .... i will try to automate this ...
thank you for your help
Sincerely

ok je ferai attention aux BBcodes !
merci pour votre réponse ....
je l'ai inséré dans mon propre script et il fonctionne sauf que je dois faire "enter " pour acquérir chaque ligne ....je vais chercher à automatiser cela ...
merci pour votre aide
cordialement
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  xml file creation from an XML file template and data from an excel file naji_python 1 2,070 Dec-21-2020, 03:24 PM
Last Post: Gribouillis
  How to close file handler or log file on each iteration Mekala 3 5,025 Aug-16-2020, 03:15 PM
Last Post: snippsat
  How to save CSV file data into the Azure Data Lake Storage Gen2 table? Mangesh121 0 2,080 Jun-26-2020, 11:59 AM
Last Post: Mangesh121
  How to update data on each iteration Raj 1 2,441 Apr-23-2018, 04:17 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020