.split() Qeustion

wxjmfauth at gmail.com wxjmfauth at gmail.com
Wed Aug 14 10:32:03 EDT 2013


Le mercredi 14 août 2013 13:55:23 UTC+2, Joshua Landau a écrit :
> On 14 August 2013 12:45, Peter Otten <__peter__ at web.de> wrote:
> 
> > Joshua Landau wrote:
> 
> >> On 14 August 2013 09:30, Alister <alister.ware at ntlworld.com> wrote:
> 
> >>> I would agree with the last statement.
> 
> >>> Please write list definitions as lists rather than taking a short-cut to
> 
> >>> save a few key presses
> 
> >>
> 
> >> That's true with this example, but is:
> 
> >>
> 
> >> lines = [
> 
> >>     "Developments in high-speed rail, and high-speed",
> 
> > ...
> 
> >>     "same problems the latter was designed to solve."
> 
> >> ]
> 
> >>
> 
> >> really more readable than:
> 
> >>
> 
> >> lines = """\
> 
> >> Developments in high-speed rail, and high-speed
> 
> > ...
> 
> >> same problems the latter was designed to solve.
> 
> >> """[1:-1].split("\n")
> 
> >>
> 
> >> ?
> 
> >
> 
> > It's definitely more correct -- unless you meant to strip the "D" from the
> 
> > first line ;)
> 
> >
> 
> > I would use
> 
> >
> 
> > lines = """\
> 
> > Developments in high-speed rail, and high-speed
> 
> > ...
> 
> > same problems the latter was designed to solve.
> 
> > """.splitlines()
> 
> 
> 
> Thanks, I didn't actually know about .splitlines()!

a = ['==\r**', '==\n**', '==\r\n**', '==\u0085**',
'==\u000b**', '==\u000c**', '==\u2028**', '==\u2029**']
for e in a:
    print(e.splitlines())
    
['==', '**']
['==', '**']
['==', '**']
['==', '**']
['==', '**']
['==', '**']
['==', '**']
['==', '**']


Do not confuse these NLF's (new line functions) in the Unicode
terminology, with the end of line *symbols* (pilcrow, \u2424, ...)


I'm always and still be suprised by the number of hard coded
'\n' one can find in Python code when the portable (here
win)

>>> os.linesep
'\r\n'

exists.

jmf



More information about the Python-list mailing list