Problem in splitting a string

Discussion in 'Python' started by Angelo Secchi, Jul 22, 2004.

  1. 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: www.sssup.it/~secchi/
    ========================================================
     
    Angelo Secchi, Jul 22, 2004
    #1
    1. Advertising

  2. Angelo Secchi

    wes weston Guest

    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: www.sssup.it/~secchi/
    > ========================================================


    Angelo,
    For example, how how would you want the example given to
    be split?
    wes
     
    wes weston, Jul 22, 2004
    #2
    1. Advertising

  3. Angelo Secchi

    wes weston Guest

    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: 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
     
    wes weston, Jul 22, 2004
    #3
  4. 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 <> 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: 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: www.sssup.it/~secchi/
    ========================================================
     
    Angelo Secchi, Jul 22, 2004
    #4
  5. Angelo Secchi

    Jens Thiede Guest

    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: 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:
    Time Zone: UTC +2
    Location: South Africa
     
    Jens Thiede, Jul 22, 2004
    #5
  6. Angelo Secchi

    Dave Brueck Guest

    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
     
    Dave Brueck, Jul 22, 2004
    #6
  7. Angelo Secchi

    Peter Otten Guest

    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
     
    Peter Otten, Jul 22, 2004
    #7
  8. Angelo Secchi

    Jeff Shannon Guest

    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
     
    Jeff Shannon, Jul 22, 2004
    #8
  9. Angelo Secchi

    Paul McGuire Guest

    "Angelo Secchi" <> wrote in message
    news:...
    >
    > 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: 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
     
    Paul McGuire, Jul 22, 2004
    #9
  10. Angelo Secchi

    Byron Guest

    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
     
    Byron, Jul 22, 2004
    #10
  11. Angelo Secchi

    Byron Guest

    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
     
    Byron, Jul 22, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Ericson
    Replies:
    0
    Views:
    435
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    449
  3. John Dibling
    Replies:
    0
    Views:
    422
    John Dibling
    Jul 19, 2003
  4. Anthony Liu

    Problem splitting a string

    Anthony Liu, Oct 15, 2005, in forum: Python
    Replies:
    8
    Views:
    325
    Kent Johnson
    Oct 15, 2005
  5. Replies:
    3
    Views:
    664
Loading...

Share This Page