Mailing List Archive

Help me split a string into elements
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: Help me split a string into elements [ In reply to ]
DFS <nospam@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.)
--
https://mail.python.org/mailman/listinfo/python-list
Re: Help me split a string into elements [ In reply to ]
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']

--
https://mail.python.org/mailman/listinfo/python-list