Writing in a document problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Writing in a document problem (/thread-38531.html) |
Writing in a document problem - IOHANNES - Oct-26-2022 Hi everyone, i'm French and i try to code in python language. In university we have to code in html/xml and i try to developp an app that put automaticaly the sentences between html tags. I'm only at the begining of said programm. There is the code : from os import chdir chdir("/home/jean/Documents/fac/info/fable_python") #I open my text document in wich is my titles in writing mode. And i create/open second one wich #will be the "html" document. folderFab=open(("liste_fable"),'r') tei=open(("liste_fable2.txt"),'w') #I try the process with only ten titles for more clarity in the results for i in range(10): t=str(folderFab.readline()) s='<title>'+ t +'</title>' tei.write(s) folderFab.close() tei.close()unfortunately it give the following result : <title>Le Dépositaire infidèle </title><title>Les Deux Pigeons </title><title>Le Singe et le Léopard </title><title>Le Gland et la Citrouille </title><title>L’Écolier, le Pédant et le Maître d’un jardin </title><title>Le Statuaire et la Statue de Jupiter </title><title>La Souris métamorphosée en fille </title><title>Le Fou qui vend la Sagesse </title><title>L'Huître et les Plaideurs </title><title>Le Loup et le Chien maigre </title> I dont know why, i wanted something looking like, for exemple : <title>Le singe et le Léopard</titles> I already tried to change the var type in string i the "s" variable. Someone have any idea ? Thanks, RE: Writing in a document problem - wavic - Oct-26-2022 Hello! There is a logical mistake here. You have a new line symbol on every line you read. So "s" is actually "<title>Le Dépositaire infidèle\n</title>". So "</title>" goes to the next line because of /n before it. And then you add to the same line in the file the next one. Try to change line 13 like that: s='<title>'+ t.strip() +'</title>'In order to understand what is going on just put a print statement somewhere in the for loop. Or more: for i in range(10): t=str(folderFab.readline()) print(t) s='<title>'+ t +'</title>' print(s) tei.write(s) RE: Writing in a document problem - IOHANNES - Oct-26-2022 thanks a lot for your fast help. I'll try i didn't knew the .strip(). RE: Writing in a document problem - snippsat - Oct-26-2022 You can loop direclty over the file object,and range() is not nessesary.Try to use with open then no need to close file obejct,also using f-sting make it nicer.Then it can be done like this. with open('list_fable.txt') as fp,open('list_out.html', 'w') as fp_out: for line in fp: fp_out.write(f'<title>{line.strip()}</title>\n')list_out.html:
RE: Writing in a document problem - IOHANNES - Oct-26-2022 (Oct-26-2022, 02:40 PM)snippsat Wrote: You can loop direclty over the file object,and very clear thanks |