Re: Split with python

Discussion in 'Python' started by Tim Chase, Aug 29, 2006.

  1. Tim Chase

    Tim Chase Guest

    Norman Khine wrote:
    > Hello,
    > I have a csv file which is has a field that has something like:
    >
    > text.csv
    > "text (xxx)"
    > "text (text) (yyy)"
    > "text (text) (text) (zzz)"
    >
    > I would like to split the last '(text)' out and put it in a new column,
    > so that I get:
    >
    > new_test.csv
    > "text","(xxx)"
    > "text (text)","(yyy)"
    > "text (text) (text)","(zzz)"
    >
    > how can this be done?


    line.rsplit(None, 1)

    seems to do the trick for me:

    >>> tests=[

    .... ("text (xxx)", ("text","(xxx)")),
    .... ("text (text) (yyy)", ("text (text)","(yyy)")),
    .... ("text (text) (text) (zzz)", ("text (text) (text)","(zzz)"))
    .... ]
    >>> for test, result in tests:

    .... r = test.rsplit(None,1)
    .... if r[0] <> result[0] or r[1] <> result[1]:
    .... print test, result
    ....

    shows that the results of rsplit() match the expected results.

    -tkc
     
    Tim Chase, Aug 29, 2006
    #1
    1. Advertising

  2. Tim Chase

    John Machin Guest

    Tim Chase wrote:
    > Norman Khine wrote:
    > > Hello,
    > > I have a csv file which is has a field that has something like:
    > >
    > > text.csv
    > > "text (xxx)"
    > > "text (text) (yyy)"
    > > "text (text) (text) (zzz)"
    > >
    > > I would like to split the last '(text)' out and put it in a new column,
    > > so that I get:
    > >
    > > new_test.csv
    > > "text","(xxx)"
    > > "text (text)","(yyy)"
    > > "text (text) (text)","(zzz)"
    > >
    > > how can this be done?

    >
    > line.rsplit(None, 1)
    >
    > seems to do the trick for me:
    >


    provided the (xxx) etc doesn't contain whitespace.
     
    John Machin, Aug 29, 2006
    #2
    1. Advertising

  3. Tim Chase

    tobiah Guest

    Tim Chase wrote:
    > Norman Khine wrote:
    >> Hello,
    >> I have a csv file which is has a field that has something like:
    >>
    >> text.csv
    >> "text (xxx)"
    >> "text (text) (yyy)"
    >> "text (text) (text) (zzz)"
    >>
    >> I would like to split the last '(text)' out and put it in a new
    >> column, so that I get:
    >>
    >> new_test.csv
    >> "text","(xxx)"
    >> "text (text)","(yyy)"
    >> "text (text) (text)","(zzz)"
    >>
    >> how can this be done?

    >
    > line.rsplit(None, 1)
    >
    > seems to do the trick for me:
    >
    > >>> tests=[

    > ... ("text (xxx)", ("text","(xxx)")),
    > ... ("text (text) (yyy)", ("text (text)","(yyy)")),
    > ... ("text (text) (text) (zzz)", ("text (text) (text)","(zzz)"))
    > ... ]
    > >>> for test, result in tests:

    > ... r = test.rsplit(None,1)
    > ... if r[0] <> result[0] or r[1] <> result[1]:
    > ... print test, result
    > ...
    >
    > shows that the results of rsplit() match the expected results.
    >
    > -tkc



    Of course, fixing the csv file takes a little more work. It sounds like the
    test lines given were just one of the fields, and there are
    the quotes to worry about.

    csvfile:
    "field1","text (xxx)","field3"
    "field1","text (text) (yyy)","field3"
    "field1","text (text) (text) (zzz)","field3"

    .................................
    import sys
    def fix(x):
    for line in open('csvfile'):
    fields = line.split(',')
    first, last = fields[x].rsplit(None, 1)
    fields[x] = first + '"'
    fields.insert(x + 1, '"' + last)
    sys.stdout.write(','.join(fields))

    fix(1)
    .................................

    "field1","text","(xxx)","field3"
    "field1","text (text)","(yyy)","field3"
    "field1","text (text) (text)","(zzz)","field3"

    But then this fails if there are commas in the
    data. I could split and join on '","' but then
    that fails when 'x' is either the first or last field.

    Are there tools in the csv module that make this
    easier?

    Tobiah

    --
    Posted via a free Usenet account from http://www.teranews.com
     
    tobiah, Aug 30, 2006
    #3
  4. Tim Chase

    John Machin Guest

    tobiah wrote:

    > Of course, fixing the csv file takes a little more work. It sounds like the
    > test lines given were just one of the fields, and there are
    > the quotes to worry about.
    >

    [snip]
    >
    > But then this fails if there are commas in the
    > data. I could split and join on '","' but then
    > that fails when 'x' is either the first or last field.
    >
    > Are there tools in the csv module that make this
    > easier?


    There are no "tools". The main (whole?) purpose of the csv module is to
    intelligently handle the embedded comma and embedded quote problems on
    both input and output. May I suggest that you read the documentation?
     
    John Machin, Aug 30, 2006
    #4
  5. At Tuesday 29/8/2006 20:31, tobiah wrote:

    > >> I have a csv file which is has a field that has something like:
    > >>

    >But then this fails if there are commas in the
    >data. I could split and join on '","' but then
    >that fails when 'x' is either the first or last field.
    >
    >Are there tools in the csv module that make this
    >easier?


    Yes, just use the csv module and forget all that split&joins...



    Gabriel Genellina
    Softlab SRL





    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
     
    Gabriel Genellina, Aug 30, 2006
    #5
  6. Tim Chase

    tobiah Guest

    John Machin wrote:
    > tobiah wrote:
    >
    >> Of course, fixing the csv file takes a little more work. It sounds like the
    >> test lines given were just one of the fields, and there are
    >> the quotes to worry about.
    >>

    > [snip]
    >> But then this fails if there are commas in the
    >> data. I could split and join on '","' but then
    >> that fails when 'x' is either the first or last field.
    >>
    >> Are there tools in the csv module that make this
    >> easier?

    >
    > There are no "tools". The main (whole?) purpose of the csv module is to
    > intelligently handle the embedded comma and embedded quote problems on
    > both input and output. May I suggest that you read the documentation?
    >


    If you read the entire thread, you may find that I am
    not deserving of a condescending reply.

    --
    Posted via a free Usenet account from http://www.teranews.com
     
    tobiah, Aug 30, 2006
    #6
  7. Tim Chase

    John Machin Guest

    tobiah wrote:
    > John Machin wrote:
    > > tobiah wrote:
    > >
    > >> Of course, fixing the csv file takes a little more work. It sounds like the
    > >> test lines given were just one of the fields, and there are
    > >> the quotes to worry about.
    > >>

    > > [snip]
    > >> But then this fails if there are commas in the
    > >> data. I could split and join on '","' but then
    > >> that fails when 'x' is either the first or last field.
    > >>
    > >> Are there tools in the csv module that make this
    > >> easier?

    > >
    > > There are no "tools". The main (whole?) purpose of the csv module is to
    > > intelligently handle the embedded comma and embedded quote problems on
    > > both input and output. May I suggest that you read the documentation?
    > >

    >
    > If you read the entire thread, you may find that I am
    > not deserving of a condescending reply.
    >


    I did read the whole thread. I have read it again. I find that given a
    question of the form "are there tools in the X module ...", a response
    suggesting that you read the documentation and failing re.search("F",
    response, re.I) is not unreasonable.

    "condescending" is your inference, not my implication.

    Cheers,
    John
     
    John Machin, Aug 30, 2006
    #7
    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. Replies:
    2
    Views:
    490
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    735
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    236
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    274
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    702
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page