Split with python

tobiah st at tobiah.org
Tue Aug 29 19:31:35 EDT 2006


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




More information about the Python-list mailing list