Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Split with python

Reply
Thread Tools

Re: Split with python

 
 
Tim Chase
Guest
Posts: n/a
 
      08-29-2006
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




 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      08-29-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:
>


provided the (xxx) etc doesn't contain whitespace.

 
Reply With Quote
 
 
 
 
tobiah
Guest
Posts: n/a
 
      08-29-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

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      08-29-2006

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?

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-30-2006
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

 
Reply With Quote
 
tobiah
Guest
Posts: n/a
 
      08-30-2006
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

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      08-30-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split on '' (and another for split -1) trans. (T. Onoma) Ruby 10 12-28-2004 06:36 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments