Python Regex replace

前端 未结 6 1322
野性不改
野性不改 2021-02-05 05:01

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

6条回答
  •  爱一瞬间的悲伤
    2021-02-05 05:41

    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.

提交回复
热议问题