How to Split a String - Python

This is a discussion on How to Split a String - Python ; Hi, I need to convert the string: '(a, b, "c", d, "e")' into the following list ['a', 'b', 'c', 'd', 'e']. Much like a csv reader does. I usually use the split function, but this mini-monster wouldn't properly get split ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

How to Split a String

  1. Default How to Split a String

    Hi,

    I need to convert the string: '(a, b, "c", d, "e")' into the following
    list ['a', 'b', 'c', 'd', 'e']. Much like a csv reader does. I usually
    use the split function, but this mini-monster wouldn't properly get
    split up due to those random quotations postgresql returns to me.

    Please help me with this,
    Thanks,
    Sia

  2. Default Re: How to Split a String

    Siah wrote:

    > I need to convert the string: '(a, b, "c", d, "e")' into the
    > following list ['a', 'b', 'c', 'd', 'e']. Much like a csv reader
    > does. I usually use the split function, but this mini-monster
    > wouldn't properly get split up due to those random quotations
    > postgresql returns to me.


    I heavily suggest you to look at the docs -- those are very basic
    functions.

    http://docs.python.org/lib/string-methods.html

    One solution might be:

    >>> results = []
    >>> for part in '(a, b, "c", d, "e")'.split(","):

    .... part = part.strip()
    .... part = part.strip("(),\"")
    .... part = part.strip()
    .... results.append(part)
    ....
    >>> print results

    ['a', 'b', 'c', 'd', 'e']
    >>>


    Regards,


    Björn


    --
    BOFH excuse #285:

    Telecommunications is upgrading.


  3. Default Re: How to Split a String

    On 2007-11-29, Siah <siasookhteh@gmail.com> wrote:

    > I need to convert the string: '(a, b, "c", d, "e")' into the following
    > list ['a', 'b', 'c', 'd', 'e']. Much like a csv reader does.


    http://docs.python.org/lib/module-csv.html

    --
    Grant Edwards grante Yow! ... the HIGHWAY is
    at made out of LIME JELLO and
    visi.com my HONDA is a barbequeued
    OYSTER! Yum!

  4. Default Re: How to Split a String

    > I need to convert the string: '(a, b, "c", d, "e")' into the
    > following list ['a', 'b', 'c', 'd', 'e']. Much like a csv
    > reader does. I usually use the split function, but this
    > mini-monster wouldn't properly get split up due to those
    > random quotations postgresql returns to me.


    Uh...use the csv reader? No need to reinvent the parsing wheel.

    >>> import csv
    >>> from StringIO import StringIO
    >>> s = 'a,b,"c",d,"e"'
    >>> csv.reader(StringIO(s)).next()

    ['a', 'b', 'c', 'd', 'e']

    If you really have the parens in your string too, you can peal
    them off first with "s[1:-1]"

    >>> s = '(a,b,"c",d,"e")'
    >>> csv.reader(StringIO(s[1:-1])).next()

    ['a', 'b', 'c', 'd', 'e']

    or strip any/all parens:

    >>> s = '(a,b,"c",d,"e")'
    >>> csv.reader(StringIO(s.lstrip('(').rstrip(')'))).next()

    ['a', 'b', 'c', 'd', 'e']

    -tkc



  5. Default Re: How to Split a String

    Siah ha scritto:
    > Hi,
    >
    > I need to convert the string: '(a, b, "c", d, "e")' into the following
    > list ['a', 'b', 'c', 'd', 'e']. Much like a csv reader does. I usually
    > use the split function, but this mini-monster wouldn't properly get
    > split up due to those random quotations postgresql returns to me.
    >
    > Please help me with this,
    > Thanks,
    > Sia


    One solution:

    >>> s = '(a, b, "c", d, "e")'
    >>> print [x.strip('" ') for x in s.strip('()').split(',')]

    ['a', 'b', 'c', 'd', 'e']

  6. Default Re: How to Split a String

    The basic split/strip method wouldn't split '(a, b, "c,...", d)',
    which is why I chose not to use it.

    The csv solution seems to work well, that helped me much here (thank
    you), but I am looking to see if I can get it solved with some regular
    expression. This is how far I've come so far, but it needs much work:
    re.compile('"*,"*').split(x[1:-1])

    Thanks for your help,
    Sia




    On Nov 29, 3:24 pm, Bjoern Schliessmann <usenet-
    mail-0306.20.chr0n...@spamgourmet.com> wrote:
    > Siah wrote:
    > > I need to convert the string: '(a, b, "c", d, "e")' into the
    > > following list ['a', 'b', 'c', 'd', 'e']. Much like a csv reader
    > > does. I usually use the split function, but this mini-monster
    > > wouldn't properly get split up due to those random quotations
    > > postgresql returns to me.

    >
    > I heavily suggest you to look at the docs -- those are very basic
    > functions.
    >
    > http://docs.python.org/lib/string-methods.html
    >
    > One solution might be:
    >
    > >>> results = []
    > >>> for part in '(a, b, "c", d, "e")'.split(","):

    >
    > ... part = part.strip()
    > ... part = part.strip("(),\"")
    > ... part = part.strip()
    > ... results.append(part)
    > ...>>> print results
    >
    > ['a', 'b', 'c', 'd', 'e']
    >
    >
    >
    > Regards,
    >
    > Björn
    >
    > --
    > BOFH excuse #285:
    >
    > Telecommunications is upgrading.



  7. Default Re: How to Split a String

    On 2007-11-29, imho <certo@comeno.it> wrote:
    > Siah ha scritto:
    >> Hi,
    >>
    >> I need to convert the string: '(a, b, "c", d, "e")' into the following
    >> list ['a', 'b', 'c', 'd', 'e']. Much like a csv reader does. I usually
    >> use the split function, but this mini-monster wouldn't properly get
    >> split up due to those random quotations postgresql returns to me.
    >>
    >> Please help me with this,
    >> Thanks,
    >> Sia

    >
    > One solution:
    >
    > >>> s = '(a, b, "c", d, "e")'
    > >>> print [x.strip('" ') for x in s.strip('()').split(',')]

    > ['a', 'b', 'c', 'd', 'e']


    That fails when a quoted string contains commas:

    >>> s = '(a, b, "c", d, "e,f,g")'
    >>> print [x.strip('" ') for x in s.strip('()').split(',')]

    ['a', 'b', 'c', 'd', 'e', 'f', 'g']

    I presume the correct result would be

    ['a', 'b', 'c', 'd', 'e,f,g']

    --
    Grant Edwards grante Yow! I wonder if I could
    at ever get started in the
    visi.com credit world?

  8. Default Re: How to Split a String

    On 2007-11-29, Grant Edwards <grante@visi.com> wrote:

    >> One solution:
    >>
    >> >>> s = '(a, b, "c", d, "e")'
    >> >>> print [x.strip('" ') for x in s.strip('()').split(',')]

    >> ['a', 'b', 'c', 'd', 'e']

    >
    > That fails when a quoted string contains commas:
    >
    >>>> s = '(a, b, "c", d, "e,f,g")'
    >>>> print [x.strip('" ') for x in s.strip('()').split(',')]

    > ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    >
    > I presume the correct result would be
    >
    > ['a', 'b', 'c', 'd', 'e,f,g']


    You can get that result easily with the csv module:

    >>> repr(ss)

    '\'a,b,"c",d,"e,f,g"\''
    >>> for row in csv.reader([ss],skipinitialspace=True):

    .... print row
    ....
    ['a', 'b', 'c', 'd', 'e,f,g']

    --
    Grant Edwards grante Yow! I'm not available
    at for comment..
    visi.com

  9. Default Re: How to Split a String

    Thanks Mr. Edwards, I went ahead and started using the csv reader.
    Sia

  10. Default Re: How to Split a String

    Grant Edwards ha scritto:

    >> One solution:
    >>
    >>>>> s = '(a, b, "c", d, "e")'
    >>>>> print [x.strip('" ') for x in s.strip('()').split(',')]

    >> ['a', 'b', 'c', 'd', 'e']

    >
    > That fails when a quoted string contains commas:
    >
    >>>> s = '(a, b, "c", d, "e,f,g")'
    >>>> print [x.strip('" ') for x in s.strip('()').split(',')]

    > ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    >
    > I presume the correct result would be
    >
    > ['a', 'b', 'c', 'd', 'e,f,g']
    >


    Uhm, agree. I supposed quoted strings were presumed to be 'trivial'.
    Definitely csv module is the solution :-)

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. How to split string
    By Application Development in forum Python
    Replies: 7
    Last Post: 12-06-2007, 02:38 AM
  2. IE and string.split('\n')
    By Application Development in forum Javascript
    Replies: 4
    Last Post: 05-29-2007, 11:36 AM
  3. String.Split or Regex
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 05-14-2007, 11:17 AM
  4. String.Split()
    By Application Development in forum DOTNET
    Replies: 2
    Last Post: 04-12-2007, 10:06 AM
  5. How to Split HTML String?
    By Application Development in forum Inetserver
    Replies: 10
    Last Post: 02-08-2007, 04:19 PM