[Tutor] My first program

Paul Yachnes Paul Yachnes" <paul.yachnes@netzero.net
Sat, 9 Sep 2000 19:55:44 -0400


This is a multi-part message in MIME format.

------=_NextPart_000_004D_01C01A97.F0D74EC0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I am trying to write a program that takes a text file (it prompts you for
the file name)  and creates a new file (adding a .ent extension) with the
same text , but converting ANSI characters to character entites. This almost
works, but it seems only to convert the even numbered lines in the file. Is
this because "oldfile.readline()" is called twice in my control loop? If so,
how do I avoid this? Could someone please take a look at this for me (file
attached). Thank you.

Paul Yachnes

P.S. I realize someone has probably already written a program like this, but
it is still a great learning experience for me.






------=_NextPart_000_004D_01C01A97.F0D74EC0
Content-Type: text/plain;
	name="ansi2ent.py"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="ansi2ent.py"

import string
print 'THIS PROGRAM CONVERTS ANSI CHARACTERS TO CHARACTER ENTITIES.\n'
file =3D raw_input('Please enter name of file to convert: ')
oldfile =3D open(file, 'r')
newfile =3D open(file + '.ent', 'w')
dict =3D {'=91':'&lsquo;', '=92':'&rsquo;', '=A1':'&iexcl;', =
'=A2':'&cent;', '=A3':'&pound;', '=A4':'&curren;',
'=A5':'&yen;', '=A6':'&brvbar;', '=A7':'&sect;', '=A8':'&uml;', =
'=A9':'&copy;', '=AA':'&ordf;', '=AB':'&laquo;',=20
'=AC':'&not;', '=AD':'&shy;', '=AE':'&reg;', '=AF':'&macr;', =
'=B0':'&deg;', '=B1':'&plusmn;', '=B2':'&sup2;',=20
'=B3':'&sup3;', '=B4':'&;acute', '=B5':'&;micro', '=B6':'&;para', =
'=B7':'&;middot', '=B8':'&;cedil',=20
'=B9':'&;sup1', '=BA':'&ordm;', '=BB':'&raquo;', '=BC':'&frac14;', =
'=BD':'&frac12;', '=BE':'&frac34;',=20
'=BF':'&iquest;', '=C0':'&Agrave;', '=C1':'&Aacute;', '=C2':'&Acirc;', =
'=C3':'&Atilde;', '=C4':'&Auml;',=20
'=C5':'&Aring;', '=C6':'&AElig;', '=C7':'&Ccedil;', '=C8':'&Egrave;', =
'=C9':'&Eacute;', '=CA':'&Ecirc;',=20
'=CB':'&Euml;', '=CC':'&Igrave;', '=CD':'&Iacute;', '=CE':'&Icirc;', =
'=CF':'&Iuml;', '=D0':'&ETH;',=20
'=D1':'&Ntilde;', '=D2':'&Ograve;', '=D3':'&Oacute;', '=D4':'&Ocirc;', =
'=D5':'&Otilde;', '=D6':'&Ouml;',=20
'=D7':'&times;', '=D8':'&Oslash;', '=D9':'&Ugrave;', '=DA':'&Uacute;', =
'=DB':'&Ucirc;', '=DC':'&Uuml;',=20
'=DD':'&Yacute;', '=DE':'&THORN;', '=DF':'&szlig;', '=E0':'&agrave;', =
'=E1':'&aacute;', '=E2':'&acirc;',=20
'=E3':'&atilde;', '=E4':'&auml;', '=E5':'&aring;', '=E6':'&aelig;', =
'=E7':'&ccedil;',  '=E8':'&egrave;',=20
'=E9':'&eacute;', '=EA':'&ecirc;', '=EB':'&euml;', '=EC':'&igrave;', =
'=ED':'&iacute;', '=EE':'&icirc;',=20
'=EF':'&iuml;', '=F0':'&eth;', '=F1':'&ntilde;', '=F2':'&ograve;', =
'=F3':'&oacute;', '=F4':'&ocirc;',=20
'=F5':'&otilde;', '=F6':'&ouml;', '=F7':'&divide;', '=F8':'&oslash;', =
'=F9':'&ugrave;', '=FA':'&uacute;',=20
'=FB':'&ucirc;', '=FC':'&uuml;', '=FD':'&yacute;', '=FE':'&thorn;', =
'=FF':'&yuml;'}
list =3D dict.items()
while oldfile.readline() !=3D "":
    line =3D oldfile.readline()
    newline =3D string.replace(line, '&', '&amp;')
    for x, y in list:
        newline =3D string.replace(newline, x, y)
    newfile.write(newline)
oldfile.close()=09
newfile.close()
------=_NextPart_000_004D_01C01A97.F0D74EC0--

_______________________________________________
Why pay for something you could get for free?
NetZero provides FREE Internet Access and Email
http://www.netzero.net/download/index.html