Ad

Split String Only After Double Quotes In Python

- 1 answer

I have a string like this here:

"BLAX", "BLAY", "BLAZ, BLUBB", "BLAP"

And yes the double quotes are within this string.

Now I want to split this string into several parts with mystring.split(",") What I got is this

"BLAX"

"BLAY"

"BLAZ

BLUBB"

"BLAP"

But what I want is this:

"BLAX"

"BLAY"

"BLAZ, BLUBB"

"BLAP"

How can I achieve this and as well I want to keep the double quotes? I need this because I work with toml files.

Solution: Thanks @Giacomo Alzetta

I used the split command with the regular expression. Thanks also for explaining this!

Ad

Answer

You can use a regular expression and the re.split function:

>>> import re
>>> re.split(r'(?<="),', '"BLAX", "BLAY", "BLAZ, BLUBB", "BLAP"')
['"BLAX"', ' "BLAY"', ' "BLAZ, BLUBB"', ' "BLAP"']

(?<=") means must be preceded by " but the " is not included in the actual match so only the , is used to actually do the splitting.

You could split by ", but then you'd have to fix up the parts where the " is now missing:

>>> '"BLAX", "BLAY", "BLAZ, BLUBB", "BLAP"'.split('",')
['"BLAX', ' "BLAY', ' "BLAZ, BLUBB', ' "BLAP"']
>>> [el + ('' if el.endswith('"') else '"') for el in '"BLAX", "BLAY", "BLAZ, BLUBB", "BLAP"'.split('",')]
['"BLAX"', ' "BLAY"', ' "BLAZ, BLUBB"', ' "BLAP"']
Ad
source: stackoverflow.com
Ad