NEWLINE character problem
nuffsaid at phreaker.net
Fri Jan 30 16:30:36 CET 2004
On Fri, 30 Jan 2004 16:16:12 +0200, Dragos Chirila wrote:
> I have a string and I want to split it by NEWLINE character.
> It is knowned that "Microsoft Windows, Apple's Macintosh OS and various
> UNIX, all use different characters to mark the ends of lines in text files.
> Unix uses the linefeed (ASCII character 10), MacOS uses the carriage return
> (ASCII character 13), and Windows uses a two-character sequence of a
> carriage return plus a newline". So, PLEASE don't tell me to use 'split' by
> '\n' because it won't work.
> I know about the Python support, opening files with 'U' flag, for reading in
> universal newline mode. But this is not the case. My script receives a
> string, can be the content of a file text from Windows or Unix system. I
> want to make sure that I can split this string by NEWLINE character.
Say your string is s; then you could use the following
function (untested!) to make sure that s uses \n only:
if txt.count('\r\n'): # MS DOS
txt = txt.replace('\r\n', '\n')
elif txt.count('\r'): # Mac
txt = txt.replace('\r', '\n')
Simply write: s = fix_lineendings(s)
HTH / Nuff
More information about the Python-list