python reg ex to include missing commas

后端 未结 2 1718
刺人心
刺人心 2021-01-15 13:22

I need to ensure an string to have comma separated values. The strings I read may have space separated values.

  • Some commas might be missing in my input string
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-15 13:28

    PyParsing will definitely not be the fastest way to run this, but it is perhaps the fastest way to write it ;-)

    from pyparsing import *
    
    STRING = sglQuotedString | dblQuotedString
    NONSTRING = Word(alphanums + '.-')
    line = OneOrMore(STRING | NONSTRING | Suppress(',')) + lineEnd
    
    
    def insert_commas(s):
        values = line.parseString(s).asList()
        return ", ".join(values)
    
    
    s1 = """1, ' unchanged 1' " unchanged  2 "  2, 2"""
    s2 = """1, ' unchanged 1', " unchanged 2 " ,  2, 2"""
    s3 = """ 1, ' unchanged 1' " unchanged 2 " 2, 2 45"""
    s4 = """1, 67.90e-34 67.90E-34 7.9093339333 2, 2 """
    
    print insert_commas(s1)
    print insert_commas(s2)
    print insert_commas(s3)
    print insert_commas(s4)
    

    which prints

    1, ' unchanged 1', " unchanged  2 ", 2, 2
    1, ' unchanged 1', " unchanged 2 ", 2, 2
    1, ' unchanged 1', " unchanged 2 ", 2, 2, 45
    1, 67.90e-34, 67.90E-34, 7.9093339333, 2, 2
    

提交回复
热议问题