regex parsing request from api python

i have python script and getting responce from api, responce style is like this

?{"res":"yes","info":{"name":"name surname \/ name surname"}}

how can i show only name and surname?

import requests
number1 = input("nomeri ")
num = "number"
params = {}
params[num]= number1

var = requests.post('api', data = params)


print(var.text)

2 answers

  • answered 2019-02-10 12:35 Devesh Kumar Singh

    If you have control over what the python api responds, you can split out name and surname in the api itself, and assign them to keys "name" and "surname"

    else do something like

    dict_obj = {"res":"yes","info":{"name":"name surname \/ name surname"}}
    name_str = dict_obj["info"]["name"].split('\/').strip()
    name_list = name_str.split(" ")
    name = name_list[0]
    surname = name_list[1]
    
    

  • answered 2019-02-10 12:57 Erick

    Assuming that this is a dictionary, you don't need regex to get name and surname. Just do:

    dic = {'res':'yes','info':{'name':"name surname \/ name surname}}
    res = dic['info']['name']
    name_surname = res{:res.index('\\')]
    

    I also wonder what the "?" is? Hope i work!