string to list

Shambhu Rajak Shambhu.Rajak at kpitcummins.com
Thu Jun 14 02:58:12 EDT 2012


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





More information about the Python-list mailing list