0

I'm getting facedetection data from an API in this form:

{"id":1,"ageMin":0,"ageMax":100,"faceConfidence":66.72220611572266,"emotion":"ANGRY","emotionConfidence":50.0'
b'2540969848633,"eyeglasses":false,"eyeglassesConfidence":50.38102722167969,"eyesOpen":true,"eyesOpenConfidence":50.20328140258789'
b',"gender":"Male","genderConfidence":50.462989807128906,"smile":false,"smileConfidence":50.15522384643555,"sunglasses":false,"sun'
b'glassesConfidence":50.446510314941406}]'

I'd like to save this to a csv-file like this:

id  ageMin  ageMax  faceConfidence
1   0       100     66

... and so on. I tried to do it this way:

response = requests.get(url, headers=headers)
    with open('detections.csv', 'w') as f:
        writer = csv.writer(f)
        for item in response:
            writer.writerow(str(item))

That puts every char in its own cell. I've also tried to use item.id, but that gives an error: AttributeError: 'bytes' object has no attribute 'id'.

Could someone point me to the right direction?

2
  • 1
    Your response is JSON-formatted. Python doesn't automatically turn JSON strings to objects, hence why you're getting that AttributeError. Parse the string with the json library before attempting to access the object's properties. Commented Aug 3, 2020 at 19:33
  • 2
    To get json response, use response.json() than reading and converting to json Commented Aug 3, 2020 at 19:35

2 Answers 2

3

Maybe an overkill for a small task, but you can do the following:

  1. convert JSON response (do not forget to check exceptions, etc.) to python dictionary

    dic = response.json()

  2. Create a dataframe, for example using pandas:

    df = pandas.DataFrame(dic)

  3. Save to csv omitting index:

    df.to_csv('detections.csv', index=False, sep="\t")

Sign up to request clarification or add additional context in comments.

Comments

3

You can do this relatively easily with the pandas and json libraries.

import pandas as pd
import json

response = """{
    "id": 1,
    "ageMin": 0,
    "ageMax": 100,
    "faceConfidence": 66.72220611572266,
    "emotion": "ANGRY",
    "emotionConfidence": 50.0,
    "eyeglasses": false,
    "eyeglassesConfidence": 50.38102722167969,
    "eyesOpen": true,
    "eyesOpenConfidence": 50.20328140258789,
    "gender": "Male",
    "genderConfidence": 50.462989807128906,
    "smile": false,
    "smileConfidence": 50.15522384643555,
    "sunglasses": false,
    "glassesConfidence":50.446510314941406
}"""

file = json.loads(doc)

json = pd.DataFrame({"data": file})
json.to_csv("response.csv")

This is the response formatted to csv.

,data
ageMax,100
ageMin,0
emotion,ANGRY
emotionConfidence,50.0
eyeglasses,False
eyeglassesConfidence,50.38102722167969
eyesOpen,True
eyesOpenConfidence,50.20328140258789
faceConfidence,66.72220611572266
gender,Male
genderConfidence,50.462989807128906
glassesConfidence,50.446510314941406
id,1
smile,False
smileConfidence,50.15522384643555
sunglasses,False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.