How to Split a String

S

Siah

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
 
B

Bjoern Schliessmann

Siah said:
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
 
T

Tim Chase

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.
['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:
['a', 'b', 'c', 'd', 'e']

-tkc
 
I

imho

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']
 
S

Siah

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




Siah said:
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
 
G

Grant Edwards

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']
 
G

Grant Edwards

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']
 
I

imho

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 :)
 
M

Marc 'BlackJack' Rintsch

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
 
B

Bjoern Schliessmann

Siah said:
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
 
S

Siah

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top