[Tutor] Problem with printing Python output to HTML Correctly
Spyros Charonis
s.charonis at gmail.com
Tue May 10 20:06:14 CEST 2011
A SOLUTION TO THE PROBLEM I POSTED:
align_file_rmode =
open('/Users/spyros/folder1/python/printsmotifs/alignfiles/' + query1, 'r')
align_file_appmode =
open('/Users/spyros/folder1/python/printsmotifs/alignfiles/' + query2, 'a+')
finalmotifs = motif_file.readlines()
seqalign = align_file_rmode.readlines()
for line in seqalign:
#align_file_appmode.write('<p> \'line\' </p>')
for item in finalmotifs:
item = item.strip().upper()
annotation = "<span style=\"color:red\">"+item+"</span>"
if item in line:
newline = line.replace(item, annotation)
# sys.stdout.write(newline)
align_file_appmode.write(newline)
motif_file.close()
align_file_rmode.close()
align_file_appmode.close()
the line
annotation = "<span style=\"color:red\">"+item+"</span>"
added a span and set the color in CSS.
On Tue, May 10, 2011 at 6:14 PM, Spyros Charonis <s.charonis at gmail.com>wrote:
> Hi all,
>
> No need to post answers, I figured out where my mistake was.
>
> Spyros
>
>
> On Tue, May 10, 2011 at 5:11 PM, Spyros Charonis <s.charonis at gmail.com>wrote:
>
>> Hello,
>>
>> I know I posted the exact same topic a few hours ago and I do apologize
>> for this, but my script had a careless error, and my real issue is somewhat
>> different.
>> I have a Python script that extracts some text from a database 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, and I would like
>> to instead write the results to HTML so that I can annotate my file in more
>> visually effective ways,e.g. by changing text color where appropriate. My
>> program extracts text from a database, reads a file that is to be annotated,
>> and writes those
>> annotations to a newly created (.htm) file.
>>
>> finalmotifs = motif_file.readlines()
>> seqalign = align_file_rmode.readlines()
>>
>> # These two files have been created in code that I don't show here because
>> it is not relevant to the issue
>>
>> align_file_appmode.write('<html>')
>> align_file_appmode.write('<head>')
>>
>> align_file_appmode.write
>> ('<title>
>> \'query_\' Multiple Sequence Alignment
>> </title>')
>>
>> align_file_appmode.write('</head>')
>> align_file_appmode.write('<body>')
>>
>> for line in seqalign:
>> align_file_appmode.write('<p> \'line\' </p>')
>> for item in finalmotifs:
>> item = item.strip().upper()
>> if item in line:
>>
>> newline = line.replace
>> (item, '<p> <font color = "red"> \'item\' </font></p>')
>>
>> align_file_appmode.write(newline)
>>
>> align_file_appmode.write('</body>')
>> align_file_appmode.write('</html>')
>>
>> motif_file.close()
>> align_file_rmode.close()
>> align_file_appmode.close()
>>
>> The .htm file that is created is not what I intend it to be, it has the
>> word "item"
>> printed every couple lines because I assume I'm not passing the string
>> sequence that I want to output correctly.
>>
>> QUESTION
>> Basically, HTML (or the way I wrote my code) does not understand that with
>> the
>> escape character '\item\' I am trying to print a string and not the word
>> "item".
>> Is there someway to correct that or would I have to use
>> something like XML to create a markup system that specifically describes
>> my data?
>>
>> I am aware Python supports multiline strings (using the format ''' text
>> ''') but I do want my HTML ( or XML?)
>> to be correctly rendered before I consider making this into a CGI program.
>> Built in python 2.6.5
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110510/a5a80cf6/attachment.html>
More information about the Tutor
mailing list