Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 read text file and write into html with correct link
#1
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:
http://www.google.com
http://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
Quote
#2
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
Quote
#3
hi avorane,
thanks it work.
Quote
#4
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
Quote
#5
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Does python guarantees that file.write() works without file.close() ? kryptomatrix 7 189 Yesterday, 11:28 AM
Last Post: kryptomatrix
  I am trying to read a pandas file Balaji 1 82 Oct-08-2019, 10:55 PM
Last Post: Larz60+
  How to read a text file into a list or an array musouka 2 204 Oct-07-2019, 01:54 PM
Last Post: musouka
  open, read and replace a string in a file Reims 0 77 Oct-02-2019, 01:30 PM
Last Post: Reims
  How do I read the HTML files in a directory and write the content into a CSV file? glittergirl 1 114 Sep-23-2019, 11:01 AM
Last Post: Larz60+
  Read each line, replace string and save into a new file igormonteiro 2 165 Sep-15-2019, 01:24 PM
Last Post: buran
  How do I write class objects to a file in binary mode? Exsul1 7 413 Sep-14-2019, 09:33 PM
Last Post: snippsat
  Open and read multiple text files and match words kozaizsvemira 2 149 Sep-11-2019, 12:58 PM
Last Post: kozaizsvemira
  How to read a file using stdin? pyth0nus3r 1 130 Aug-24-2019, 01:14 AM
Last Post: Larz60+
  how do i write a cross-platform relative file path? pseudo 2 128 Aug-23-2019, 05:07 PM
Last Post: ThomasL

Forum Jump:


Users browsing this thread: 1 Guest(s)