Split by comma and how to exclude comma from quotes in split … Python

后端 未结 8 1950
长情又很酷
长情又很酷 2020-12-16 13:28

python 2.7 code

cStr = \'\"aaaa\",\"bbbb\",\"ccc,ffffd\"\' 

newStr = cStr.split(\',\')

print newStr 

# result : [\'\"aaaa\"\',\'\"bbbb\"\',\'\"ccc\',\'ffffd\         


        
相关标签:
8条回答
  • 2020-12-16 13:29

    You can first split the string by " then filter out '' or ',', finally format it, it may be the simplest way:

    ['"%s"' % s for s in cStr.split('"') if s and s != ',']
    
    0 讨论(0)
  • 2020-12-16 13:30

    This is not a standard module, you have to install it via pip, but as an option try tssplit:

    In [3]: from tssplit import tssplit
    In [4]: tssplit('"aaaa","bbbb","ccc,ffffd"', quote='"', delimiter=',')                                                            
    Out[4]: ['aaaa', 'bbbb', 'ccc,ffffd']
    
    0 讨论(0)
  • 2020-12-16 13:31

    pyparsing has a builtin expression, commaSeparatedList:

    cStr = '"aaaa","bbbb","ccc,ffffd"' 
    import pyparsing as pp
    print(pp.commaSeparatedList.parseString(cStr).asList())
    

    prints:

    ['"aaaa"', '"bbbb"', '"ccc,ffffd"']
    

    You can also add a parse-time action to strip those double-quotes (since you probably just want the content, not the quotation marks too):

    csv_line = pp.commaSeparatedList.copy().addParseAction(pp.tokenMap(lambda s: s.strip('"')))
    print(csv_line.parseString(cStr).asList())
    

    gives:

    ['aaaa', 'bbbb', 'ccc,ffffd']
    
    0 讨论(0)
  • 2020-12-16 13:31

    By using regex try this:

    COMMA_MATCHER = re.compile(r",(?=(?:[^\"']*[\"'][^\"']*[\"'])*[^\"']*$)")
    split_result = COMMA_MATCHER.split(string)
    

    0 讨论(0)
  • 2020-12-16 13:43

    The solution using re.split() function:

    import re
    
    cStr = '"aaaa","bbbb","ccc,ffffd"'
    newStr = re.split(r',(?=")', cStr)
    
    print newStr
    

    The output:

    ['"aaaa"', '"bbbb"', '"ccc,ffffd"']
    

    ,(?=") - lookahead positive assertion, ensures that delimiter , is followed by double quote "

    0 讨论(0)
  • 2020-12-16 13:43

    Try to use CSV.

    import csv
    cStr = '"aaaa","bbbb","ccc,ffffd"'
    newStr = [ '"{}"'.format(x) for x in list(csv.reader([cStr], delimiter=',', quotechar='"'))[0] ]
    
    print newStr
    

    Check Python parse CSV ignoring comma with double-quotes

    0 讨论(0)
提交回复
热议问题