Python Forum

Full Version: read text file and write into html with correct link
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
hi avorane,
thanks it work.
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
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.