如何基于逗号(',')拆分字符串而不考虑方括号('('和')')中的逗号?

时间:2019-02-14 18:52:22

标签: python-3.x

我想使用带有逗号的python 3+分割字符串。我不希望根据括号内的逗号分割字符串。

例如:-

 cstr = 'animal_tiger,(CAST(SUBSTR(TRIM(replace(MAX(tigers_name),"Body Parts",'')),1,3) AS INT))'

我想将其拆分为两个字符串元素。

我尝试了基于逗号的拆分,但是它也包含了逗号。

 import re
 import csv 
 from StringIO import StringIO 
 cstr = 'animal_tiger,(CAST(SUBSTR(TRIM(replace(MAX(tigers_name),"Body Parts",'')),1,3) AS INT))'

 b = re.split(r',(?=")', cstr)
 print(b)

 c = re.split(''',(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''', cstr)
 print(c)

 data = StringIO(cstr) 
 reader = csv.reader(data, delimiter=';') 
 for row in reader: 
     print(row) 

 def split_with_commas_outside_of_quotes(string):
     arr = []
     start, flag = 0, False
     for pos, x in enumerate(string):
        if x == '(' and x == ')':
            flag= not(flag)
        if flag == False and x == ',':
            arr.append(string[start:pos])
            start = pos+1
    arr.append(string[start:pos])
    return arr

 print(split_with_commas_outside_of_quotes(cstr))

 print(cstr.replace('(','$')).replace(')','#').split(',')

预期结果是将字符串分成两个不同的列表字符串:-

 outputlist - ['animal_tiger','(CAST(SUBSTR(TRIM(replace(MAX(tigers_name),"Body Parts",'')),1,3) AS INT))']

记住列表的长度是2。

2 个答案:

答案 0 :(得分:2)

您在这里。使用此功能:

def split_with_commas_outside_of_quotes(string):
    arr = []
    bracketCount = 0
    currentItem = ""
    for i in range(len(string)):
        if i == len(string)-1:
            currentItem += string[i]
            arr.append(currentItem)
        elif string[i] == "(":
            bracketCount += 1
            currentItem += string[i]
        elif string[i] == ")":
            bracketCount -= 1
            currentItem += string[i]
        elif bracketCount == 0 and string[i] == ",":
            arr.append(currentItem)
            currentItem = ""
        else:
            currentItem += string[i]
    return arr


cstr = 'animal_tiger,(CAST(SUBSTR(TRIM(replace(MAX(tigers_name),"Body Parts",'')),1,3) AS INT))'
print(split_with_commas_outside_of_quotes(cstr))

输出:

['animal_tiger', '(CAST(SUBSTR(TRIM(replace(MAX(tigers_name),"Body Parts",)),1,3) AS INT))']

答案 1 :(得分:0)

您可以使用split()

data = """animal_tiger,(CAST(SUBSTR(TRIM(replace(MAX(tigers_name),"Body Parts",'')),1,3) AS INT))"""
data.split(',', 1)
>>> ['animal_tiger',
     '(CAST(SUBSTR(TRIM(replace(MAX(tigers_name),"Body Parts",\'\')),1,3) AS INT))']