Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Split with python

 
 
Norman Khine
Guest
Posts: n/a
 
      08-29-2006
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?

Thanks

Norman
 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      08-30-2006
Norman Khine:
> I have a csv file which is has a field that has something like:
> "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:
> "text","(xxx)"
> "text (text)","(yyy)"
> "text (text) (text)","(zzz)"



Maybe something like this can be useful, after few improvements (RE
formatting is a work in progress):

from StringIO import StringIO
import re

datain = StringIO("""
"text (xxx)"
"text (text) (yy y) "
"text (text) (text) ( zzz ) "
""")


lastone = re.compile("""
\s* ( \(
[^()"]*
\)
\s* "
)
\s* $
""", re.VERBOSE)

def repl(mobj):
txt_found = mobj.groups()[0]
return '", "' + txt_found

for line in datain:
line2 = line.strip()
if line2:
print lastone.sub(repl, line2)

"""
The output is:

"text", "(xxx)"
"text (text)", "(yy y) "
"text (text) (text)", "( zzz ) "

"""


Bye,
bearophile

 
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