Python Forum
Writing in a document problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing in a document problem
#1
Bug 
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,
Reply
#2
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) 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
thanks a lot for your fast help. I'll try i didn't knew the .strip().
Reply
#4
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:
Output:
<title>Le Dépositaire infidèle</title> <title>Les Deux Pigeons</title> <title>Le Singe et le Léopard</title>
wavic and IOHANNES like this post
Reply
#5
(Oct-26-2022, 02:40 PM)snippsat Wrote: 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:
Output:
<title>Le Dépositaire infidèle</title> <title>Les Deux Pigeons</title> <title>Le Singe et le Léopard</title>

very clear thanks Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem writing dataframe to oracle aliyesami 4 2,705 Sep-25-2021, 11:20 PM
Last Post: SamHobbs
  Problem writing a variable value to Excel Chuck_Norwich 1 1,976 Jul-25-2019, 02:20 PM
Last Post: Chuck_Norwich
  Problem with reading and writing to file. darktitan 2 2,319 Jul-20-2019, 06:06 PM
Last Post: darktitan
  Writing Answers From Questionnaire to a new .txt Document FizzyBreak579 4 3,604 Feb-16-2018, 07:26 AM
Last Post: buran

Forum Jump:

User Panel Messages

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