Ad
Split String Only After Double Quotes In Python
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
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad