[Tutor] To write data in two different fonts?

Nick Raptis airscorp at otenet.gr
Wed Aug 12 11:51:15 CEST 2009


Your main concern when displaying plain text in HTML is actually only 5 
characters:
&, <, >, ", '
which you should escape(replace) with their coresponding HTML entities.
Here's a few lines of code to give you an idea how to do it:

--------------------------------------------------------
#put this somewhere in the beginning of your script,
#tailor it as you like
entities = {'&' : '&amp;',
            '<' : '&lt;',
            '>' : '&gt;',
            '"' : '&quot;',
            "'" : '&apos;'}

......

#put this in the file crunching loop
#I'll use *line* as the text variable, like it would happen
#in a *for line in file* scenario
    ....
    for character, entity in entities.items():
         line = line.replace(character, entity)
    .....
    ..... #do the rest of your stuff with the line
-------------------------------------------------------

That's a simple implementation and it actually performs 5 replacements 
on every line of your file (which
is not as slow as you might think though) but should get you started if 
you need it.

You also might want to consider escaping other characters as well (maybe 
spaces to preserve whitespace without a <pre>)
Get your hands on an online HTML entity reference online.

Extra points:
Include a CSS file with your HTML output to handle representation.
With some more lines in your script you can then code different fonts or 
sizes according to any logic you want.
Won't go into explaining HTML though (hint: use the class HTML attribute)

If it sounds more complex than you'd like, it's ok. It's more powerful 
and more rewarding too.
Once you get the basic structure in, you can extend your script to do 
pretty much anything with your text.

Nick


Alan Gauld wrote:
>
> "prasad rao" <prasadaraon50 at gmail.com> wrote
>> I put in tag <pre> and it worked. But at one particular line in a module
>> the font changed colour.After that all the lines are in blue colour and
>> under lined.
>
> How are you viewing the file?
> As Kent poinred out pure text files have no fonts or colours.
> So the blue colour must be coming from the tyool you are using to 
> display the contents. It is interpreting sometjhing in the data as a 
> command to switch format. Since you are using HTML
> it is probably a <a> tag. Have you checked the data for any <a 
> sequences (or and other < characters for that matter)?
>
>
>> figure out why the change of colour of font took place.
>
> Open it in a simple text editor like notepad or nano and see what it 
> looks like.
>
> HTH,
>
>


More information about the Tutor mailing list