-3

I have this string which is a response from 2captcha

{'captchaId': '69775358180', 'code':
'coordinates:x=100,y=285;x=147,y=299;x=226,y=316;x=262,y=131'}

how can I extract each x,y coordinate and save them to variable? I've played around with regex but cant quite figure it out.

Thanks

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
Mshea
  • 1
  • 1
    this looks like a dictionary, not a string –  Mar 10 '22 at 17:49
  • 1
    Are you sure that's a string? Can you provide the ``repr`` of it? Or are you asking just about the ``'coordinates:...'`` part? – MisterMiyagi Mar 10 '22 at 17:52
  • @MisterMiyagi. Good catch – Mad Physicist Mar 10 '22 at 17:53
  • What do you mean "save then to variable"? Do you want a list of integers? A list of x, y tuples? A bunch of individual variables (probably not a good idea)? Something else? What are you trying to accomplish? – CrazyChucky Mar 10 '22 at 18:01
  • The problem statement isn't clear yet. if you are talking about "'coordinates:x=100,y=285;x=147,y=299;x=226,y=316;x=262,y=131'" as string because in question it looks like dictionary to me. Also, This problem is related to only this particular response string or you need to write a code that needs to be written in a general manner for any values having the different values for x,y coordinates in the same pattern string. – Shubhank Gupta Mar 10 '22 at 18:05
  • I found something similar to your problem. "https://stackoverflow.com/questions/4666973/how-to-extract-the-substring-between-two-markers" You can try this. It might solve your issue. – Shubhank Gupta Mar 10 '22 at 18:21
  • Im trying to save each x,y coordinate the response gives to a variable( if thats the correct thing , im new to python) so i can use theses coordinates to click on a certain place on screen. – Mshea Mar 10 '22 at 18:44

2 Answers2

0

Heres a code to get the x and y coordinates and store them in their own list:

data ={'captchaId': '69775358180', 'code': 'coordinates:x=100,y=285;x=147,y=299;x=226,y=316;x=262,y=131'}

#this gets the 'x=100,y=285;x=147,y=299;x=226,y=316;x=262,y=131'
coordinates = data['code'].split(':')[1].split(';')

x_coordinates = []
y_coordinates = []

for pair in coordinates:
  xy = pair.split(',')
  x_coordinates.append(xy[0][2:])
  y_coordinates.append(xy[1][2:])

print(x_coordinates) # ['100', '147', '226', '262']
print(y_coordinates) # ['285', '299', '316', '131']

If you need a single x or y coordinate just access them using the index of the list.

-1

If you have a dictionary you could use this code to create a list of the co-ordinates as tuples.

data = {'captchaId': '69775358180', 'code':
'coordinates:x=100,y=285;x=147,y=299;x=226,y=316;x=262,y=131'}

coords =[(xy.split(',')[0][2:],xy.split(',')[1][2:] ) for xy in data['code'][12:].split(";")]

print(coords)
[('100', '285'), ('147', '299'), ('226', '316'), ('262', '131')]

If you wanted numeric co-ordinates:

coords =[(int(xy.split(',')[0][2:]),int(xy.split(',')[1][3:]) ) for xy in data['code'][12:].split(";")]
[(100, 85), (147, 99), (226, 16), (262, 31)]
norie
  • 9,609
  • 2
  • 11
  • 18
  • brilliant , thanks. Is there anyway to save the coords as a single ? as in [(100), (85) , (147) , (99) etc etc so when i use print(coords[0]) it will be 100 and not 100,85 – Mshea Mar 10 '22 at 20:51
  • You can use `coords[0][0]` to get the first x co-ordinate and `coords[0][1]` to get the first y co-ordinate and so on. Also, having the co-ordinates as tuples in a list could actually be useful depending on your next step. – norie Mar 11 '22 at 08:25