end of lines

Eric Mattes ericmattes at yahoo.com
Tue Jan 28 14:15:49 EST 2003


Peter, you are the genious of geniouses! Thank you very much!

I'm glad that I at least had the right idea :^)
-E

"Peter Hansen" <peter at engcorp.com> wrote in message
news:3E36D40A.3333769F at engcorp.com...
> Eric Mattes wrote:
> >
> > I am running Windows XP and activestate python 2.2.1. I'm writing a
> > program that reads in a (local) file using urllib and the readlines()
> > method like so:
> >
> > code = "".join(urllib.urlopen(file).readlines())
> > string.replace(code, "\r", "\n")
>
> Strings are immutable!  You need to bind a name to the result of the call
> to replace().  You meant to do this instead:
>
> code = string.replace(code, '\r', '\n')
>
> If you're using Python 2.2.1 though, you can just do this and avoid
> the import statement.
>
> code = code.replace('\r', '\n')
>
> > compiled_code = compile(code, '<string>', 'exec')
> >
> > This causes a SyntaxError "Invalid Syntax" with an arrow pointing at
> > the end of the first line of the string read from the file. For
> > example, the file's first line is:
>
> Another possibility is that you need to add a newline at the end of the
> string.  Try this instead:
>
> code = code.replace('\r', '\n') + '\n'
>
> -Peter






More information about the Python-list mailing list