Removing newlines from string on windows (without replacing)

Ulrich Eckhardt doomster at knuut.de
Sun Jul 26 05:54:55 EDT 2009


Tom wrote:
> s = sauce.replace("\n", "")
> 
> Sauce is a string, read from a file, that I need to remove newlines
> from. This code works fine in Linux, but not in Windows. rstrip("\n")
> won't work for me, so anybody know how to get this working on Windows?

I'm pretty sure this works regardless of the system. What might change is 
the way that a line is terminated: MS Windows by default uses a CR/LF 
('\r\n') pair, while most other systems just an LF ('\n').

I'd adapt the code to accept either line ending on either system.

If that's not what you want, you should improve your "doesn't work" 
description. ;)

Uli





More information about the Python-list mailing list