Problem in splitting a string

A

Angelo Secchi

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: (e-mail address removed) www.sssup.it/~secchi/
========================================================
 
W

wes weston

Angelo said:
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: (e-mail address removed) www.sssup.it/~secchi/
========================================================

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

wes weston

Angelo said:
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: (e-mail address removed) 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
 
A

Angelo Secchi

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



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


--
========================================================
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: (e-mail address removed) www.sssup.it/~secchi/
========================================================
 
J

Jens Thiede

Angelo said:
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: (e-mail address removed) 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.
 
D

Dave Brueck

Angelo said:
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
 
P

Peter Otten

Angelo said:
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
 
J

Jeff Shannon

Angelo said:
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
 
P

Paul McGuire

Angelo Secchi said:
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: (e-mail address removed) 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
 
B

Byron

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
 
B

Byron

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
 

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

Similar Threads

Converting binaty to ascii 4
problem with struct module 2
Help with dates 1
Blocking the execution of a script 1
Newbie lost 12
Little explanation 2
Help in STRING compare ? 3

Members online

No members online now.

Forum statistics

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

Latest Threads

Top