Hey I\'m trying to figure out a regular expression to do the following.
Here is my string
Place,08/09/2010,\"15,531\",\"2,909\",650
I
You could parse a string of that format using pyparsing:
import pyparsing as pp
import datetime as dt
st='Place,08/09/2010,"15,531","2,909",650'
def line_grammar():
integer=pp.Word(pp.nums).setParseAction(lambda s,l,t: [int(t[0])])
sep=pp.Suppress('/')
date=(integer+sep+integer+sep+integer).setParseAction(
lambda s,l,t: dt.date(t[2],t[1],t[0]))
comma=pp.Suppress(',')
quoted=pp.Regex(r'("|\').*?\1').setParseAction(
lambda s,l,t: [int(e) for e in t[0].strip('\'"').split(',')])
line=pp.Word(pp.alphas)+comma+date+comma+quoted+comma+quoted+comma+integer
return line
line=line_grammar()
print(line.parseString(st))
# ['Place', datetime.date(2010, 9, 8), 15, 531, 2, 909, 650]
The advantage is you parse, convert, and validate in a few lines. Note that the ints are all converted to ints and the date to a datetime structure.