By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,932 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

How to Split a String

P: n/a
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
Nov 29 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
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.

Nov 29 '07 #2

P: n/a
On 2007-11-29, Siah <si*********@gmail.comwrote:
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!
Nov 29 '07 #3

P: n/a
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
Nov 29 '07 #4

P: n/a
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']
Nov 29 '07 #5

P: n/a
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.comwrote:
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.
Nov 29 '07 #6

P: n/a
On 2007-11-29, imho <ce***@comeno.itwrote:
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?
Nov 29 '07 #7

P: n/a
On 2007-11-29, Grant Edwards <gr****@visi.comwrote:
>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
Nov 29 '07 #8

P: n/a
Thanks Mr. Edwards, I went ahead and started using the csv reader.
Sia
Nov 29 '07 #9

P: n/a
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 :-)
Nov 29 '07 #10

P: n/a
On Thu, 29 Nov 2007 12:12:20 -0800, 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 hope you don't use Python to access the database, get a tuple back,
convert it to a string and then try to break up that string into a list!?

Ciao,
Marc 'BlackJack' Rintsch
Nov 30 '07 #11

P: n/a
Siah wrote:
The basic split/strip method wouldn't split '(a, b, "c,...", d)',
which is why I chose not to use it.
Could you please explain which part of my example doesn't work?
split takes arguments which enables it to split your string as
desired.
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])
Have fun. Would be too easy without regexps, wouldn't it?

Regards,
Björn

--
BOFH excuse #382:

Someone was smoking in the computer room and set off the halon
systems.

Nov 30 '07 #12

P: n/a
I hope you don't use Python to access the database, get a tuple back,
convert it to a string and then try to break up that string into a list!?
Sadly, that is the case. Well, kinda. I'm using psycopg2 to access
postgresql, which is great. Though postgres has more features than
psycopg2 supports, such as nested arrays within a row, that psycopg2
provides to me as a string, which leaves it to me to try to make sense
out of it. I hate it, its dirty and uncool, but oh well.

Sia
Nov 30 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.