The inverse of .join

Jon Clements joncle at googlemail.com
Fri Jun 18 11:35:10 EDT 2010


On 17 June, 21:03, Neil Cerutti <ne... at norwich.edu> wrote:
> On 2010-06-17, Robert Kern <robert.k... at gmail.com> wrote:
>
> > On 6/17/10 2:08 PM, Neil Cerutti wrote:
> >> On 2010-06-17, Ian Kelly<ian.g.ke... at gmail.com>  wrote:
> >>> On Thu, Jun 17, 2010 at 11:45 AM, Neil Cerutti
> >>> <ne... at norwich.edu>  wrote:
> >>>> What's the best way to do the inverse operation of the .join
> >>>> function?
>
> >>> Use the str.split method?
>
> >> split is perfect except for what happens with an empty string.
>
> > 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.
>
> --
> Neil Cerutti

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']]))
[]

hth
Jon.




More information about the Python-list mailing list