Help me split a string into elements

DFS nospam at dfs.com
Sat Sep 4 18:16:46 EDT 2021


On 9/4/2021 5:55 PM, DFS 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


I got it:

lines = [('one\ntwo\nthree\n',)]
print(str(lines[0][0]).splitlines())
['one', 'two', 'three']



More information about the Python-list mailing list