string to list

Discussion in 'Python' started by bruce g, Jun 14, 2012.

  1. bruce g

    bruce g Guest

    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
    bruce g, Jun 14, 2012
    #1
    1. Advertising

  2. bruce g

    Chris Rebert Guest

    Chris Rebert, Jun 14, 2012
    #2
    1. Advertising

  3. 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:]
    Sent: 14/06/2012 8:00 AM
    To:
    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
    Shambhu Rajak, Jun 14, 2012
    #3
  4. bruce g

    Peter Otten Guest

    bruce g wrote:

    > 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’]


    >>> import csv
    >>> next(csv.reader(['AAA,"BBBB,CCCC,DDDD",EEE,FFF,GGG']))

    ['AAA', 'BBBB,CCCC,DDDD', 'EEE', 'FFF', 'GGG']

    For multiple records:

    list(csv.reader(text.splitlines(True)))
    Peter Otten, Jun 14, 2012
    #4
  5. bruce g

    Chris Rebert Guest

    On Thu, Jun 14, 2012 at 12:40 AM, Hemanth H.M <> wrote:
    >>>> list(literal_eval('"aa","bb 'b'","cc"'))

    > ['aa', 'bb ', 'cc']
    >
    > Strange?


    Not really. You didn't properly escape the embedded quotation marks in
    the string itself!
    So before anything ever even gets passed to literal_eval(), that part
    is parsed as two adjacent literals: '"aa","bb ' and b'","cc"'
    In Python 3.x, the "b" prefix indicates a `bytes` literal rather than
    a `str` literal.

    Implicit adjacent string literal concatenation then occurs.
    Thus:
    >>> print '"aa","bb ' b'","cc"'

    "aa","bb ","cc"
    Compare:
    >>> print '''"aa","bb 'b'","cc"'''

    "aa","bb 'b'","cc"

    But really, literal_eval() should not be used for CSV; it won't handle
    unquoted fields at all, among other issues.

    Cheers,
    Chris
    Chris Rebert, Jun 14, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. roopa
    Replies:
    6
    Views:
    723
    Jerry Coffin
    Aug 27, 2004
  2. dackz
    Replies:
    0
    Views:
    474
    dackz
    Feb 6, 2007
  3. Debajit Adhikary
    Replies:
    17
    Views:
    674
    Debajit Adhikary
    Oct 18, 2007
  4. OW Ghim Siong
    Replies:
    2
    Views:
    382
    Peter Otten
    Nov 30, 2010
  5. asil klin
    Replies:
    28
    Views:
    1,276
    Andreas Leitgeb
    Mar 5, 2011
Loading...

Share This Page