Help me split a string into elements

Neil nddtwentyone at gmail.com
Sat Sep 4 18:15:12 EDT 2021


DFS <nospam at dfs.com> wrote:
> Typical cases:
>  lines = [('one\ntwo\nthree\n')]
>  print(str(lines[0]).splitlines())
>  ['one', 'two', 'three']
> 
>  lines = [('one two three\n')]
>  print(str(lines[0]).split())
>  ['one', 'two', 'three']
> 
> 
> That's the result I'm wanting, but I get data in a slightly different 
> format:
> 
> lines = [('one\ntwo\nthree\n',)]
> 
> Note the comma after the string data, but inside the paren. 
> splitlines() doesn't work on it:
> 
> print(str(lines[0]).splitlines())
> ["('one\\ntwo\\nthree\\n',)"]
> 
> 
> I've banged my head enough - can someone spot an easy fix?
> 
> Thanks

lines[0][0].splitlines()

(You have a list containing a tuple containing the string you want to
split up.)


More information about the Python-list mailing list