NEWLINE character problem

D

Dragos Chirila

Hi

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.

Any idea will be appreciated.
Thanks a lot.

Dragos
 
N

Nuff Said

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:

def fix_lineendings(txt):
if txt.count('\r\n'): # MS DOS
txt = txt.replace('\r\n', '\n')
elif txt.count('\r'): # Mac
txt = txt.replace('\r', '\n')

return txt

Simply write: s = fix_lineendings(s)

HTH / Nuff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top