Need help converting text to csv format

Joe Strout joe at strout.net
Fri Nov 21 11:03:37 EST 2008


On Nov 21, 2008, at 8:18 AM, Chuck Connors wrote:

> The first value (large number) is the UPC, the next element is the
> coupon description, followed by a date in parenthesis.  Those are the
> only three elements I am concerned with.  Can someone help me in
> reformatting this:
>
> 409220000003 Life Fitness Products $1 (12-13-08) (CVS)

Well, you could use regex to parse out those three parts... on the  
other hand, str.partition might be a little easier if you're not  
already used to regex.  Try this:

 >>> line = "409220000003 Life Fitness Products $1 (12-13-08) (CVS)"
 >>> num,sep,rest = line.partition(' ')
 >>> num
'409220000003'
 >>> rest
'Life Fitness Products $1 (12-13-08) (CVS)'
 >>> desc,sep,rest = rest.partition('(')
 >>> desc
'Life Fitness Products $1 '
 >>> rest
'12-13-08) (CVS)'
 >>> date,sep,rest = rest.partition(')')
 >>> date
'12-13-08'

So three lines of code gets you your three fields.

> into something like this:
>
> "409220000003","Life Fitness Products $1","12-13-08"

There are lots of ways to do that.  Here's one:

 >>> qfields = ['"' + fld.strip() + '"' for fld in (num,desc,date)]
 >>> out = qfields.join(',')
 >>> out
'"409220000003","Life Fitness Products $1 ","12-13-08"'

Cheers,
- Joe

P.S. Pay no attention to the grumpy graybeards who want to drive new  
users away from Python.  There are quite a few friendly and helpful  
people here, too.  :)




More information about the Python-list mailing list