Joe
Joe

Reputation: 13111

how to split string into array on commas but ignore commas in parentheses

I have a string with 3 lines:

a VARCHAR(20),
b FLOAT, c FLOAT,
d NUMBER(38,0), e NUMBER(38,0)

Need to split a string into array based on comma delimiter but to ignore commas in parentheses.

Final output is array with 5 elements:

s_arr = ['a VARCHAR(20)', 'b FLOAT', 'c FLOAT', 'd NUMBER(38,0)', 'e NUMBER(38,0)']

So far I have s_arr = s.split(",")

How to achieve that?

Upvotes: 1

Views: 1620

Answers (4)

pylang
pylang

Reputation: 44525

Using list comprehensions and string methods:

Given

s = """\
a VARCHAR(20),
b FLOAT, c FLOAT,
d NUMBER(38,0), e NUMBER(38,0)
"""

Code

[z.strip() for y in [x.split(", ") for x in s.split(",\n")] for z in y]
# ['a VARCHAR(20)', 'b FLOAT', 'c FLOAT', 'd NUMBER(38,0)', 'e NUMBER(38,0)']

Alternatively

[z.strip(",") for y in [x.split(", ") for x in s.splitlines()] for z in y]
# ['a VARCHAR(20)', 'b FLOAT', 'c FLOAT', 'd NUMBER(38,0)', 'e NUMBER(38,0)']

Upvotes: 0

ergonaut
ergonaut

Reputation: 7057

If you know more about the data, you can easily avoid all the weird parsing by doing this:

a.replace(", ", "@").replace(",\n", "@").split("@")

Which replaces the delimiters with a different character and splits them on that. This assumes you have a space after the comma for delimiters. Not the most elegant, but will handle most cases if you're in a bind.

Upvotes: 0

Andria
Andria

Reputation: 5075

Use a Regular Expression to split based on multiple delimiters

stringToSplit = '''a VARCHAR(20),
b FLOAT, c FLOAT,
d NUMBER(38,0), e NUMBER(38,0)'''

import re
re.split(', |,\n', stringToSplit)

This works because your string doesn't have any spaces or newlines after commas in the parentheses (1,2).

Upvotes: 1

user3483203
user3483203

Reputation: 51155

You may use ,(?![^\(]*[\)]) with a list comprehension:

s = '''
a VARCHAR(20),
b FLOAT, c FLOAT,
d NUMBER(38,0), e NUMBER(38,0)
'''

[i.strip() for i in re.split(r',(?![^\(]*[\)])', s)]
# ['a VARCHAR(20)', 'b FLOAT', 'c FLOAT', 'd NUMBER(38,0)', 'e NUMBER(38,0)']

Upvotes: 5

Related Questions