Ad
How To Get The Weight And Quantity From Strings In A List In Python?
I'm new to Python, so I'm confused on how to write the regex pattern to find the corresponding weights and quantities from the following list of strings.
This is what I have been doing so far.
import re
string1 = [' (Expiry Date: 30 May 2019) 4 x Organic Infant Goat Milk'
' Follow-on Formula 3 400g',
' (Expiry on 30 May 2019) 4 x Organic Infant Goat Milk'
' Follow-on Formula 2 400g ',
" [ Bellamy's ] Bellamys Organic Step 3 Toddler Milk Drink"
" 900g x 6 tins Made In Australia CARTON DEAL EXPIRE"
" 06/2019 to 2020",
' [[1+1]] FRISO (2) 1.8kg+900g',
" [[Carton Sales]] Bellamy's Organic Follow-On Formula"
" Step 2 900g x 6tins",
' Dumex Mamil Gold Stage 4 Growing Up Kid Milk Formula'
' (850g) x 6',
' Wyeth S-26 Promise Gold Stage 4 1.6kg X 6 Tins']
m = [re.search('([0-9.]+[kgG]{1,2})', s).group(0) for s in string1]
print m
My output is like this:
['400g', '400g', '900g', '1.8kg', '900g', '850g', '1.6kg']
But I would like to get this output:
['4x400g', '4x400g', '900gx6', '1.8kg+900g', '900gx6', '850gx6', '1.6kgX6']
Is there any way to get this?
Ad
Answer
It's better to normalize the quantity in the front:
m = ['x'.join(i for i in re.search(r'^(?=.*?(?:(\d+)\s*x\b|\bx\s*(\d+)))?(?=.*?((?:\b[0-9]+(?:\.[0-9]+)?(?:kg|g)\b\s*?\+?\s*?)+))', s, flags=re.IGNORECASE).groups() if i) for s in string1]
Given your sample input, m
would become:
['4x400g', '4x400g', '6x900g', '1.8kg+900g', '6x900g', '6x850g', '6x1.6kg']
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