Home > database >  How do I only get a part of the response as a string
How do I only get a part of the response as a string

Time:10-13

How can I get only a part of the response I get back in form of a string?

Code:

Status1 = r.text

Response text:

{
 "status" : "DUPLICATE"
}
Status1 = Status1.replace('"', "")
Status1 = Status1.replace("{", "")
Status1 = Status1.replace("}", "")

New response:

  status : DUPLICATE

but if I put in the follwing code it still does not work/seems to be not the same. How come?

A = "status : DUPLICATE"
if A==Status1:
print("True")

I only need the word DUPLICATE from the whole response, so is there any way to get only this word so the if statement works?

CodePudding user response:

your response seems to be json formatted, so you should be able to get a python object directly with r.json

otherwise do

import json
obj = json.loads(r.text)
print obj['status']

CodePudding user response:

Just parse the input as json, and it will convert it to a python dictionary:

response_json = r.json()

if response_json["status"] == "DUPLICATE"
    print("True")

CodePudding user response:

Try using

 import json
 response_object = json.loads(r.text)
 response_object["status"]

enter image description here

  • Related