Shiva Krishna Bavandla
Shiva Krishna Bavandla

Reputation: 26668

How to collect only the required integers from a string by matching specific word in python

Suppose i had the following string

str = 'http://www.example.com/servlet/av/jd?ai=782&ji=2553120&sn=I'

I want to get only the integer number for ji that is 255312 becasue there may be different number generating for ji in the above url

Thanks in advance......

Upvotes: 0

Views: 87

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601489

Parse the URL with urlparse:

>>> import urlparse
>>> url = 'http://www.example.com/servlet/av/jd?ai=782&ji=2553120&sn=I'
>>> query_string = urlparse.urlparse(url).query
>>> query_dict = urlparse.parse_qs(query_string)
>>> query_dict
{'ai': ['782'], 'ji': ['2553120'], 'sn': ['I']}
>>> int(query_dict['ji'])
2553120

Now you can easily retrieve the desired values.

Upvotes: 6

Related Questions