string to list

Anoop Thomas Mathew atmb4u at gmail.com
Thu Jun 14 03:28:15 EDT 2012


Hi,

You can use literal_eval from ast package.

>>> from ast import literal_eval
>>> list(literal_eval("'aa','bb','cc'")

this will return ['aa', 'bb', 'cc']

Thanks,
Anoop Thomas Mathew

atm
___
Life is short, Live it hard.




On 14 June 2012 12:28, Shambhu Rajak <Shambhu.Rajak at kpitcummins.com> wrote:

> This will do you job:
>
> >>> a = 'AAA,"BBBB,CCCC,DDDD",EEE,FFF,GGG'
> >>> b = []
> >>> for x in a.split(','):
> ...     if (x.find("\"") > -1):
> ...         x = x.strip("\"")
> ...     b.append(x)
>
> If you want reduce the lines of code u can go for this option:
> b = [x.strip("\"") for x in a.split(',')]
>
>
> So Just Cheerz,
> -Shambhu
>
> -----Original Message-----
> From: bruce g [mailto:bruceg113355 at gmail.com]
> Sent: 14/06/2012 8:00 AM
> To: python-list at python.org
> Subject: string to list
>
> What is the best way to parse a CSV string to a list?
>
> For example, how do I parse:
>    'AAA,"BBBB,CCCC,DDDD",EEE,FFF,GGG'
> to get:
>    ['AAA','BBB,CCC,DDDD','EEE','FFF','GGG']
>
> Thanks,
> Bruce
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120614/e0bb327a/attachment.html>


More information about the Python-list mailing list