python selenium seperating values

Question:

I am getting a response like this id=51555263943&code=q15cd225s6s8 and I want to define the value of the id and the value of the code in separate strings
How can I do this in python selenium maybe with regex(which I don’t completely understand it)?

Asked By: CatChMeIfUCan

||

Answers:

You can split the response on the ‘&’ value and assign the returned list elements to new variables.

input = 'id=51555263943&code=q15cd225s6s8'.split('&')

id = input[0].split('=')[1]

code = input[1].split('=')[1]

print(id)
print(code)

Output:

51555263943 
q15cd225s6s8
Answered By: Captain Caveman

Assuming the strings always come like that and that you are using regex:

import re
s = 'id=51555263943&code=q15cd225s6s8'
output = re.findall(r'(?<==)[^&]+',s,flags=re.IGNORECASE)

output is a list containing the id and the code you need.

Answered By: Denis Alves

I would also use the split method to create a list based on "&".
Afterwards I would loop over the list and do a split on "=" and store these values in a dictionary, so they are easy to access later. This way you can also easily scale to a bigger response.

res = 'id=51555263943&code=q15cd225s6s8'
resSplit = res.split('&')

resDict = {}

for i in resSplit:
  i_split = i.split('=')
  resDict[i_split[0]] = i_split[1]

print(resDict)
>>>> output
{'id': '51555263943', 'code': 'q15cd225s6s8'}
Answered By: AlbaTroels
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.