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 ...
-
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
-
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.
-
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!
-
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
-
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']
-
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.
-
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?
-
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
-
Re: How to Split a String
Thanks Mr. Edwards, I went ahead and started using the csv reader.
Sia
-
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 :-)
Similar Threads
-
By Application Development in forum Python
Replies: 7
Last Post: 12-06-2007, 02:38 AM
-
By Application Development in forum Javascript
Replies: 4
Last Post: 05-29-2007, 11:36 AM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 05-14-2007, 11:17 AM
-
By Application Development in forum DOTNET
Replies: 2
Last Post: 04-12-2007, 10:06 AM
-
By Application Development in forum Inetserver
Replies: 10
Last Post: 02-08-2007, 04:19 PM