Chaban33
Chaban33

Reputation: 1382

Removing part of the string and adding to another

i have strings like this

str1 = "https://web2.some.com/hbh/productImages?itemId=5986546"
str2 = "https://web2.some.com/hbh/productImages?itemId=5986546231"
str3 = "https://web2.some.com/hbh/productImages?itemId=22432"

how can i add only last numbers "5986546" , "5986546231" , "22432" to other string.

I mean I just need to remove somehow "https://web2.some.com/hbh/productImages?itemId=" part from strings. And length on this number can vary of course.

Upvotes: 0

Views: 64

Answers (4)

jpp
jpp

Reputation: 164843

For a single parameter, you can use urllib.parse from the standard library:

from urllib.parse import urlparse

str1 = "https://web2.some.com/hbh/productImages?itemId=5986546"

item1 = urlparse(str1).query.split('=')[-1]  # '5986546'

For multiple parameters, you can construct a dictionary via urllib.parse_qs:

from urllib.parse import urlparse, parse_qs

str2 = "https://web2.some.com/hbh/productImages?itemId=5986546&somevar=5"

args = parse_qs(urlparse(str2).query)
item2 = args['itemId']  # '5986546'

Upvotes: 2

Rarblack
Rarblack

Reputation: 4664

Use regex:

import re 

str1 = "https://web2.some.com/hbh/productImages?itemId=5986546"
str2 = "https://web2.some.com/hbh/productImages?itemId=5986546231"
str3 = "https://web2.some.com/hbh/productImages?itemId=22432"

regex = re.compile(r'(\d+?)$')
l = regex.findall(str1)
print(l)

Output:

C:\Users\Desktop>py x.py
['5986546']

Moreover, the below code will return all in once:

all_strings = ''.join( [str1,str2,str3])

regex = re.compile(r'(\d{2,})')
l = regex.findall(all_strings)
print(l)

Output:

C:\Users\Desktop>py x.py
['5986546', '5986546231', '22432']

Upvotes: 0

Mehrdad Pedramfar
Mehrdad Pedramfar

Reputation: 11083

As your URLs do not include more than one =, you can use str.split

id = str1.split('=')[-1] # or [1] in this case no diff

Upvotes: 0

emendez
emendez

Reputation: 440

Use the split function on the string.

str1.split("https://web2.some.com/hbh/productImages?itemId=")[-1]

Upvotes: 0

Related Questions