end of lines
Peter Hansen
peter at engcorp.com
Tue Jan 28 14:03:38 EST 2003
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