Python Forum
read text file and write into html with correct link
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:
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
Reply
#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
Reply
#3
hi avorane,
thanks it work.
Reply
#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
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 542 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Last record in file doesn't write to newline gonksoup 3 364 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,782 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  write to csv file problem jacksfrustration 11 1,368 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,304 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 579 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  Need to replace a string with a file (HTML file) tester_V 1 698 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  read file txt on my pc to telegram bot api Tupa 0 1,047 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,043 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,160 Jun-19-2023, 02:12 PM
Last Post: DosAtPython

Forum Jump:

User Panel Messages

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