[Tutor] To write data in two different fonts?
Dave Angel
davea at ieee.org
Wed Aug 12 19:20:42 CEST 2009
prasad rao wrote:
>>> xml.sax.saxutils.escape(/data/[, /entities/])
>>>
>>> Escape '&', '<', and '>' in a string of data.
>>>
>> > You can escape other strings of data by passing a dictionary as the
>> >optional /entities/ parameter. The keys and values must all be
>> >strings; each key will be replaced with its corresponding value. The
>> >characters '&', '<' and '>' are always escaped, even if /entities/
>> >is provided.
>>
>>
>>> Let us know if that doesn't do the trick.
>>>
>>> DaveA
>>>
>
>
> Thank you Dave
>
> It is working perfectly with the modifications you suggested.
>
> <code>
>
>
> def sc(adir):
> entities = {'&' : '&',
> '<' : '<',
> '>' : '>',
> '"' : '"',
> "'" : '''}
>
> import os,myfiles,xml.sax.saxutils
> dest=open('C:/scripts.html','w')
> s=myfiles.myfiles(adir)
> dest.write('<html>')
> for x in s:
> if os.path.isfile(x):
> sorce=open(x,'r')
>
> dest.write('<h1><font=Helvetica,fontsize=14>')
> dest.write(x)
> dest.write('</h1><pre>')
>
>
> for l in sorce:
> l=xml.sax.saxutils.escape(l,entities)
> dest.write('<font=Arial,fontsize=8>')
> dest.write(l)
> dest.write('<pre>')
> sorce.close()
> else:pass
> dest.write('</html>')
> dest.close()
>
>
There are still a few problems. First one is that you have two
translations going on. You only want to list those things in 'entities'
that aren't already done in the escape() function already. So get rid
of entities, unless you come up with some other character that needs
translating besides ampersand, less-than, and greater-than.
Second is that you have <pre> at begin and end of file, while the latter
should be </pre>, to close the <pre> section.
Besides that, you have lots of illegal html going on, which most browser
will ignore. But you might as well get it as close as you can. Note
that your font logic was just plain wrong. But you don't need it, since
it looked good when it was broken anyway.
The following produced valid xhtml, according to S3 tester, when used
with my files.
import os,xml.sax.saxutils
import myfiles
BOILERPLATE ="""\
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
My Python Scripts
</title>
</head>
<body>
"""
def sc(adir):
dest=open('M:/scripts.html','w')
filelist = myfiles.myfiles(adir)
dest.write(BOILERPLATE)
for filename in filelist:
if os.path.isfile(filename):
sorce=open(filename,'r')
dest.write('<h1>')
dest.write(filename)
dest.write('</h1><pre>')
for line in sorce:
line=xml.sax.saxutils.escape(line)
dest.write(line)
dest.write('</pre>')
sorce.close()
else:pass
dest.write('</body></html>')
dest.close()
DaveA
More information about the Tutor
mailing list