Python Forum
read text file and write into html with correct link - 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: read text file and write into html with correct link (/thread-20051.html)



read text file and write into html with correct link - jacklee26 - Jul-25-2019

Do anyone know how to read a file, and write into html with the correct link page.

i have a file name test.txt, which contain:
www.google.com
www.cnn.com

If i write as below, it will not link to correct link page. Do anyone know why? What shall i correct, if i want to link correct place.

with open('test.txt') as f,open('out.html', 'w') as f_out:
    for line in f:
        line = line.strip()
        
        #print("<a href=",line ,"</a>","link")
        #print (line1)
       
        f_out.write('{}<br>'.format('<a href="line">Link </a>'))
        
        hyperlink_format = '<a href="{link}">{text}</a>'
        
	
How to i let it display on html which will have this two link.

i also try with this
f_out.write('{}<br>'.format('<a href="+line+">Link </a>'))
same


RE: read text file and write into html with correct link - avorane - Jul-25-2019

Hello,

Try

with open('test.txt') as f,open('out.html', 'w') as f_out:
    for line in f:
        line = line.strip()
         
        #print("<a href=",line ,"</a>","link")
        #print (line1)
        
        f_out.write('{}<br>'.format('<a href="' + line + '">Link ' + line + '</a>'))
         
        hyperlink_format = '<a href="{link}">{text}</a>'
Nicolas TATARENKO


RE: read text file and write into html with correct link - jacklee26 - Jul-25-2019

hi avorane,
thanks it work.


RE: read text file and write into html with correct link - jacklee26 - Aug-01-2019

hi avorane,
i have one question why i press the link it won't link direct to http://www.google.com, it link file:///C:/read_file/www.google.com.
do you know where to change


RE: read text file and write into html with correct link - jacklee26 - Aug-02-2019

i know what's the problem, if the text file contain http:// will work, but if you put without https:// will occur file:///C:/read_file/www.google.com.