Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

python - How to separate the values that was got from Azure as request response?

I here insert all the codes. So plz anyone help me to separate the values under the Column which named as "Scored Labels". e.g. either 1 or 0. Here basically I'm passing X axis, Y axis and Z axis values as request using urllib in Python. For that I'm getting response as bunch of list. Here I only need "Scored Labels" data only for other use.

Here is the sample request code

    {
  "Inputs": {
    "input1": {
      "ColumnNames": [
        "X axis",
        "Y axis",
        "Z axis"
      ],
      "Values": [
        [
          "0",
          "0",
          "0"
        ],
        [
          "0",
          "0",
          "0"
        ]
      ]
    }
  },
  "GlobalParameters": {}
}

Input Parameters

And here is the sample response

{
  "Results": {
    "output1": {
      "type": "DataTable",
      "value": {
        "ColumnNames": [
          "X axis",
          "Y axis",
          "Z axis",
          "Scored Labels",
          "Scored Probabilities"
        ],
        "ColumnTypes": [
          "Numeric",
          "Numeric",
          "Numeric",
          "Numeric",
          "Numeric"
        ],
        "Values": [
          [
            "0",
            "0",
            "0",
            "0",
            "0"
          ],
          [
            "0",
            "0",
            "0",
            "0",
            "0"
          ]
        ]
      }
    }
  }
}

Output Parameters

All together sample code will be like this(This given in Request Response API Documentation for my Machine learning model web service)

import urllib2
# If you are using Python 3+, import urllib instead of urllib2

import json 


data =  {

        "Inputs": {

                "input1":
                {
                    "ColumnNames": ["X axis", "Y axis", "Z axis"],
                    "Values": [ [ "0", "0", "0" ], [ "0", "0", "0" ], ]
                },        },
            "GlobalParameters": {
}
    }

body = str.encode(json.dumps(data))

url = 'https://ussouthcentral.services.azureml.net/workspaces/b5eeedd29d564074b47a9c8ce5faa745/services/e6065c9017d0426196db3bbf0f78e9da/execute?api-version=2.0&details=true'
api_key = 'abc123' # Replace this with the API key for the web service
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}

req = urllib2.Request(url, body, headers) 

try:
    response = urllib2.urlopen(req)

    # If you are using Python 3+, replace urllib2 with urllib.request in the above code:
    # req = urllib.request.Request(url, body, headers) 
    # response = urllib.request.urlopen(req)

    result = response.read()
    print(result) 
except urllib2.HTTPError, error:
    print("The request failed with status code: " + str(error.code))

    # Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
    print(error.info())

    print(json.loads(error.read()))  

I'm getting output as

This get b'{"Results":{"output1":{"type":"table","value":{"ColumnNames":["X axis","Y axis","Z axis","Scored Labels","Scored Probabilities"],"ColumnTypes":["Double","Double","Double","Int32","Double"],"Values":[["0.202999","9.345999","-2.979","1","0.999358713626862"],["-0.15099999","9.378999","-2.8689999","0","1.45975587173552E-08"]]}}}}'

Process finished with exit code 0

For my this input -->. (I only need 4th parameter as either 0 or 1)

"Inputs": {

        "input1":
            {
                "ColumnNames": ["X axis", "Y axis", "Z axis"],
                "Values": [["0.202999", "9.345999", "-2.9790000"], ["-0.15099999", "9.378999", "-2.8689999"], ]
                # "Values": [["0.202999", "9.345999", "-2.9790000"], ]

            }, },
    "GlobalParameters": {
    }

Please help me solve this issue. Thanks in advance.

question from:https://stackoverflow.com/questions/65931838/how-to-separate-the-values-that-was-got-from-azure-as-request-response

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could try the below snippet/

In the below snippet replace the x or store x with your output response.

import json

x = b'{"Results":{"output1":{"type":"table","value":{"ColumnNames":["X axis","Y axis","Z axis","Scored Labels","Scored Probabilities"],"ColumnTypes":["Double","Double","Double","Int32","Double"],"Values":[["0.202999","9.345999","-2.979","1","0.999358713626862"],["-0.15099999","9.378999","-2.8689999","0","1.45975587173552E-08"]]}}}}'
#print(x)
y= json.loads(x)
output = y["Results"]
for value in output["output1"]["value"]["Values"]:
    print(value[3])

Sample Output : strong text


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...