FAQ
All,

I have the following unicode object:
u'3,"Some, text",more text'

and I want to split it into a list like this:
[3,"Some, text", more text]

In other words I want to split on the comma but not if it's inside a
double-quote.

Thanks.

Search Discussions

  • Tim Golden at Apr 13, 2011 at 3:04 pm

    On 13/04/2011 15:59, Jonno wrote:
    I have the following unicode object:
    u'3,"Some, text",more text'

    and I want to split it into a list like this:
    [3,"Some, text", more text]

    In other words I want to split on the comma but not if it's inside a
    double-quote.
    You want the csv module which is designed for *exactly* this
    form of data.

    TJG
  • Andrea Crotti at Apr 13, 2011 at 4:22 pm

    Jonno <jonnojohnson at gmail.com> writes:

    All,

    I have the following unicode object:
    u'3,"Some, text",more text'

    and I want to split it into a list like this:
    [3,"Some, text", more text]

    In other words I want to split on the comma but not if it's inside a
    double-quote.

    Thanks.
    I'm not sure how but also this seems to work:
    In[20]: s = '2,"some, text",more text'

    In [21]: re.split(r'(?<=">),', s)
    Out[21]: ['2,"some, text",more text']

    I just wanted to try the lookahead functions, which I never use but
    sometimes might come handy.
  • Andrea Crotti at Apr 13, 2011 at 4:24 pm

    Andrea Crotti <andrea.crotti.0 at gmail.com> writes:

    I'm not sure how but also this seems to work:
    In[20]: s = '2,"some, text",more text'

    In [21]: re.split(r'(?<=">),', s)
    Out[21]: ['2,"some, text",more text']

    I just wanted to try the lookahead functions, which I never use but
    sometimes might come handy.
    Eh eh no sorry I misread the output, it's a list with one element in
    this case...
    So anyway I suppose you can look ahead or before if there is a " and
    don't match the "," in this case, that was the main idea.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 13, '11 at 2:59p
activeApr 13, '11 at 4:24p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase