N
Neil Cerutti
What's the best way to do the inverse operation of the .join
function?
function?
Neil said:What's the best way to do the inverse operation of the .join
function?
What's the best way to do the inverse operation of the .join
function?
..split, possibly, although there will be problems if the string containsNeil said:What's the best way to do the inverse operation of the .join
function?
Use the str.split method?
I see what you mean.Neil said:split is perfect except for what happens with an empty string.
['']>>> ','.join(['']) ''
>>> ''.split(',')
['']>>> ','.join([]) ''
>>> ''.split(',')
split is perfect except for what happens with an empty string.
I see what you mean.Neil said:split is perfect except for what happens with an empty string.
This is consistent:
['']','.join(['']) ''
''.split(',')
but this isn't:
['']','.join([]) ''
''.split(',')
An empty string could be the result of .join(['']) or .join([]).
Should .split grow an addition keyword argument to specify the desired
behaviour? (Although it's simple enough to define your own function.)
Why don't you try it and find out?
What's the best way to do the inverse operation of the .join function?
['a', 'b', 'c', 'd', 'e']L = ["a", "b", "c|d", "e"]
s = '|'.join(L)
s 'a|b|c|d|e'
s.split('|')
Should .split grow an addition keyword argument to specify the desired
behaviour?
(Although it's simple enough to define your own function.)
I'm currently using the following without problems, while reading a data
file. One of the fields is a comma separated list, and may be empty.
f = rec['codes']
if f == "":
f = []
else:
f = f.split(",")
I just wondered if something smoother was available.
I'm currently using the following without problems, while
reading a data file. One of the fields is a comma separated
list, and may be empty.
f = rec['codes']
if f == "":
f = []
else:
f = f.split(",")
I just wondered if something smoother was available.
Seems pretty smooth to me. What's wrong with it? I assume
you've put it into a function for ease of use and reduction of
code duplication.
You could also use the ternary operator, in which case it's a
mere two- liner and short enough to inline wherever you need
it:
f = rec['codes']
f = f.split(",") if f else []
Why don't you try it and find out?
I'm currently using the following without problems, while reading
a data file. One of the fields is a comma separated list, and may
be empty.
f = rec['codes']
if f == "":
f = []
else:
f = f.split(",")
I just wondered if something smoother was available.
[]rec = { 'code1': '1,2,3', 'code2': '' }
next(csv.reader([rec['code1']])) ['1', '2', '3']
next(csv.reader([rec['code2']]))
I just wondered if something smoother was available.
In terms of behaviour and 'safety', I'd go for:
[]rec = { 'code1': '1,2,3', 'code2': '' }
next(csv.reader([rec['code1']])) ['1', '2', '3']
next(csv.reader([rec['code2']]))
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.