From: jwither on 7 Sep 2009 01:29 Given a string (read from a file) which contains raw escape sequences, (specifically, slash n), what is the best way to convert that to a parsed string, where the escape sequence has been replaced (specifically, by a NEWLINE token)? James Withers
From: Sean DiZazzo on 7 Sep 2009 01:57 On Sep 6, 10:29 pm, "jwither" <jwit...(a)sxder4kmju.com> wrote: > Given a string (read from a file) which contains raw escape sequences, > (specifically, slash n), what is the best way to convert that to a parsed > string, where the escape sequence has been replaced (specifically, by a > NEWLINE token)? > > James Withers I believe "\n" is a newline. As is "\r\n" and "\r". Choose your demon. mystring = mystring.replace("\n", demon) FYI. If you are reading from a file, you can iterate over the lines without having to worry about newlines: fi = open(path_to_file, 'r') for line in fi: process_line(line) ~Sean
From: 7stud on 7 Sep 2009 02:44 On Sep 6, 11:29 pm, "jwither" <jwit...(a)sxder4kmju.com> wrote: > Given a string (read from a file) which contains raw escape sequences, > (specifically, slash n), what is the best way to convert that to a parsed > string, where the escape sequence has been replaced (specifically, by a > NEWLINE token)? > > James Withers 1) What is a "parsed string"? 2) What is a "NEWLINE token"?
From: Chris Rebert on 7 Sep 2009 01:49 On Sun, Sep 6, 2009 at 10:29 PM, jwither<jwither(a)sxder4kmju.com> wrote: > Given a string (read from a file) which contains raw escape sequences, > (specifically, slash n), what is the best way to convert that to a parsed > string, where the escape sequence has been replaced (specifically, by a > NEWLINE token)? There's probably a more general method covering all the escape sequences, but for just \n: your_string = your_string.replace("\\n", "\n") Cheers, Chris -- http://blog.rebertia.com
From: jwither on 7 Sep 2009 03:17
"Chris Rebert" <clp2(a)rebertia.com> wrote in message news:mailman.1075.1252306208.2854.python-list(a)python.org... > On Sun, Sep 6, 2009 at 10:29 PM, jwither<jwither(a)sxder4kmju.com> wrote: >> Given a string (read from a file) which contains raw escape sequences, >> (specifically, slash n), what is the best way to convert that to a parsed >> string, where the escape sequence has been replaced (specifically, by a >> NEWLINE token)? > > There's probably a more general method covering all the escape > sequences, but for just \n: > > your_string = your_string.replace("\\n", "\n") > > Cheers, > Chris > -- > http://blog.rebertia.com Thanks! (the others are more likely to be errors than deliberate anyway) James Withers |