How to get a specific numeric or any element from a string – Python?

Question:

Here I have a string.

word = '10-7-2022 product_id:10 order_id:2 cost:500'

Here, I need to extract only the product_id’s and order_id’s value which is after ‘:’.
Kindly let me know how to make it happen.

Asked By: Devein Clara

||

Answers:

If the space is between the date and the product_id and between the product_id & order_id. Then we can split it like this:

# Out string
word = '10-7-2022 product_id:10 order_id:2 cost:500'

# Spliting the string
_, product_id_raw, order_id_raw, _ = word.split(' ')

#Extracting the values!
product_id = product_id_raw.split(':')[1]

order_id = order_id_raw.split(':')[1]

# print
print(product_id, order_id)

Answered By: Prabhas Kumar

One other method is to use regex with named groups:

import re
reg = re.compile(r"(?P<product_id>(?<=product_id:)d+).*(?P<order_id>(?<=order_id:)d+)")
word = '10-7-2022 product_id:10 order_id:2 cost:500'
result = reg.search(word)
result.group('product_id')
result.group('order_id')

If not every string contain product_id: or order_id: then you can create a more general regex, and test if there is a result.

EDIT

Also getting the date, assuming that it would always be in the order of d-m-y with or without leading zeros:

import re
reg = re.compile(r"(?P<date>d+-d+-d+).*(?P<product_id>(?<=product_id:)d+).*(?P<order_id>(?<=order_id:)d+)")
word = '10-7-2022 product_id:10 order_id:2 cost:500'
result = reg.search(word)
result.group('date')

You can also get a datetime object:

from datetime import datetime
datetime.strptime(result.group('date'), r'%d-%m-%Y')
>>> datetime.datetime(2022, 7, 10, 0, 0)
Answered By: 3dSpatialUser
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.