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

Problem in splitting a string

P: n/a

Hi,
I have string of numbers and words like

',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'

and I would like to split (I'm using string.split()) it using comma as
separator but I do not want to split in two also the "name,surname"
field. In other word I would like python in separating fields to skip
that particular comma.

How can I do that?

Thanks in advance

Angelo

--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======
Jul 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Angelo Secchi wrote:
Hi,
I have string of numbers and words like

',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'

and I would like to split (I'm using string.split()) it using comma as
separator but I do not want to split in two also the "name,surname"
field. In other word I would like python in separating fields to skip
that particular comma.

How can I do that?

Thanks in advance

Angelo

--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======


Angelo,
For example, how how would you want the example given to
be split?
wes

Jul 18 '05 #2

P: n/a


Thanks, Wes. I would like the tring in my example splitted as:

['','','',and so on,'23','','','"name,surname"','','','',and so on,'\n']

Thanks again

angelo

On Thu, 22 Jul 2004 18:23:56 GMT
wes weston <ww*****@att.net> wrote:
Angelo Secchi wrote:
Hi,
I have string of numbers and words like

',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'

and I would like to split (I'm using string.split()) it using comma
as separator but I do not want to split in two also the
"name,surname" field. In other word I would like python in
separating fields to skip that particular comma.

How can I do that?

Thanks in advance

Angelo

--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======


Angelo,
For example, how how would you want the example given to
be split?
wes

--
http://mail.python.org/mailman/listinfo/python-list

--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======
Jul 18 '05 #3

P: n/a
Angelo Secchi wrote:
Hi,
I have string of numbers and words like

',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'

and I would like to split (I'm using string.split()) it using comma as
separator but I do not want to split in two also the "name,surname"
field. In other word I would like python in separating fields to skip
that particular comma.

How can I do that?

Thanks in advance

Angelo

--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======

Angelo,
list = ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'.split (',')
print list ['', '', '', '', '', '', '23', '', '', 'asd', '', '', '', '', '"name', 'surname"', '', '', '', '', '', '', '\n']


Could you split the strings; then recombine list[14] + ',' + list[15] ?
wes

Jul 18 '05 #4

P: n/a
Angelo Secchi wrote:

Hi,
I have string of numbers and words like

',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'

and I would like to split (I'm using string.split()) it using comma as
separator but I do not want to split in two also the "name,surname"
field. In other word I would like python in separating fields to skip
that particular comma.

How can I do that?

Thanks in advance

Angelo

--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======


You may want to look at regular expressions, but in the example you gave,
you just have to split the string at the quotation marks, then every second
item in the resulting list, was between quotes:
x = ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'
x.split('"') [',,,,,,23,,,asd,,,,,', 'name,surname', ',,,,,,,\n'] '"gsdfg"sdfg' '"gsdfg"sdfg' z = '"gsdfg"sdfg'
z.split('"') ['', 'gsdfg', 'sdfg']


Regular expressions:
http://docs.python.org/lib/node109.html

A Regular Expressions Tutorial:
http://www.amk.ca/python/howto/regex/

Hope That Helped,

Jens.

--
Jabber ID: jt***@jabberafrica.org
Time Zone: UTC +2
Location: South Africa
Jul 18 '05 #5

P: n/a
Angelo Secchi wrote:

Thanks, Wes. I would like the tring in my example splitted as:

['','','',and so on,'23','','','"name,surname"','','','',and so on,'\n']


Python 2.3 has the csv module. See section 12.20.5 of the docs for some
examples.

-Dave
Jul 18 '05 #6

P: n/a
Angelo Secchi wrote:

Hi,
I have string of numbers and words like

',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'

and I would like to split (I'm using string.split()) it using comma as
separator but I do not want to split in two also the "name,surname"
field. In other word I would like python in separating fields to skip
that particular comma.

How can I do that?


A minimal example using the csv module:

import csv
import cStringIO as stringio

stream = stringio.StringIO(
"""\
,,,,,,23,,,asd,,,,,"name,surname",,,,,,,
,,,,,,23,,,asd,,,,,"jo,black",,,,,,,
,,,,,,23,,,asd,,,,,"will,do",,,,,,,
""")
# could be:
# stream = file("tmp.csv")
for record in csv.reader(stream):
print record

Peter

Jul 18 '05 #7

P: n/a
Angelo Secchi wrote:
Hi,
I have string of numbers and words like

',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'

and I would like to split (I'm using string.split()) it using comma as
separator but I do not want to split in two also the "name,surname"
field. In other word I would like python in separating fields to skip
that particular comma.


You may be able to use regular expressions (the re module) to hack
something together that'll work for your particular case. In the
general case, though, splitting delimiter-separated values in the
presence of quoting and escaping is a nontrivial problem.

I remember some discussion, quite a while ago, about several people
working on a DSV module that would handle all of this stuff, but I don't
know what became of that.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #8

P: n/a
"Angelo Secchi" <se****@sssup.it> wrote in message
news:ma*************************************@pytho n.org...

Hi,
I have string of numbers and words like

',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'

and I would like to split (I'm using string.split()) it using comma as
separator but I do not want to split in two also the "name,surname"
field. In other word I would like python in separating fields to skip
that particular comma.

How can I do that?

Thanks in advance

Angelo

--
================================================== ======
Angelo Secchi PGP Key ID:EA280337
================================================== ======
Current Position:
Graduate Fellow Scuola Superiore S.Anna
Piazza Martiri della Liberta' 33, Pisa, 56127 Italy
ph.: +39 050 883365
email: se****@sssup.it www.sssup.it/~secchi/
================================================== ======


Using pyparsing's commaSeparatedList (designed specifically to handle this
case):

from pyparsing import commaSeparatedList
testdata = ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'
print commaSeparatedList.parseString( testdata )

Gives:
['', '', '', '', '', '', '23', '', '', 'asd', '', '', '', '',
'"name,surname"', '', '', '', '', '', '', '']

Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul
Jul 18 '05 #9

P: n/a
HI Angelo,

Here's a function that will take care of what you need:

-----------

def splitrecord(stringRec):
theRec = string.split(stringRec, ',') # Split record into a list.
name = theRec[14] # Store name in memory.
surname = theRec[15] # Store surname in memory.
name = name[1:] # Trim quote from name.
surname = surname[0:len(surname)-1] # Trim quote from surname.
theRec[14] = "%s, %s" % (name, surname) # Merge and store name,
surname in list.
del(theRec[15]) # Remove list item 15 from list. Not needed.
return theRec # Return list to user.

-----------

To use this list, do the following:

recordstring = ',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'
newRecord = splitrecord(recordstring)
print newRecord

Result is:

['', '', '', '', '', '', '23', '', '', 'asd', '', '', '', '', 'name,
surname', '', '', '', '', '', '', '\n']

Hope this helps,

Byron
---
Angelo Secchi wrote:
Hi,
I have string of numbers and words like

',,,,,,23,,,asd,,,,,"name,surname",,,,,,,\n'

and I would like to split (I'm using string.split()) it using comma as
separator but I do not want to split in two also the "name,surname"
field. In other word I would like python in separating fields to skip
that particular comma.

How can I do that?

Thanks in advance

Angelo

Jul 18 '05 #10

P: n/a
Angelo,

Here is a shortened version of the function:

def splitrecord(stringRec):
theRec = string.split(stringRec, ',')
name = theRec[14][1:]
surname = theRec[15][0:len(theRec[15])-1]
theRec[14] = "%s, %s" % (name, surname)
del(theRec[15])
return theRec

----
Byron
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.