473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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
Nov 29 '07 #1
12 1952
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
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
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
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
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
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
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
Thanks Mr. Edwards, I went ahead and started using the csv reader.
Sia
Nov 29 '07 #9
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.