开发者

Writing output of a python script to HTML with CGI

开发者 https://www.devze.com 2023-03-03 09:39 出处:网络
I have a Python script that extracts some text from a file and annotates another file, writing the results to a new file. Because the files I am annotating are ASCII, I am very restricted as to how I

I have a Python script that extracts some text from a file and annotates another file, writing the results to a new file. Because the files I am annotating are ASCII, I am very restricted as to how I can annotate the text, an开发者_运维技巧d I would like to instead write the results to HTML so that I can annotate by changing text color in certain parts. The part of the text file I annotate is just a string of characters (e.g. ACTFNKJLKD), which occurs repeatedly in the text file I am reading from. The part of the program that finds the string, updates is as follows:

print "Content-type:text/html\r\n\r\n"     #header of cgi file


file1 = motif_file.readlines()
file2 = align_file_rmode.readlines() 

for line in file2: 
   for item in file1:      # "item" is the string sequence that occurs in the text file
      item = item.strip().upper()   
      if item in line:

     line = line.replace(item, 'REPLACE ITEM BY BLUE FONT' * len(item))
     <p> <bold> item </bold> </p>
     <font color="sky blue">item</font>
     align_file_amode.write(line) 

Is there a way to specify how to change color (e.g. blue) for the length of my item while outpute that to HTML? Is a CGI necessary for this type of job? I am working in Python 2.6.5


I think you want

line = line.replace(item, "<span style=\"color:skyBlue\">"+item+"</span>")

This will just add a <span> and set the color in CSS.

0

精彩评论

暂无评论...
验证码 换一张
取 消