|
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/
================================================== ====== | |
Share:
|
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 | | |
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/
================================================== ====== | | |
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 | | |
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 | | |
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 | | |
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 | | |
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 | | |
"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 | | |
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 | | |
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 | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
14 posts
views
Thread by Luka Milkovic |
last post: by
|
2 posts
views
Thread by Trint Smith |
last post: by
|
20 posts
views
Thread by Opettaja |
last post: by
|
2 posts
views
Thread by shadow_ |
last post: by
|
6 posts
views
Thread by jephperro |
last post: by
|
3 posts
views
Thread by David |
last post: by
| |
3 posts
views
Thread by Marc 'BlackJack' Rintsch |
last post: by
|
9 posts
views
Thread by Salad |
last post: by
| | | | | | | | | | |