Split with python

N

Norman Khine

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
 
B

bearophileHUGS

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top