R0bert
R0bert

Reputation: 557

Data extraction from a list of tuples of dictionaries in python

I'm trying to retrieve the users data from my an appsflyer API and the output of the API is as follows:

EDIT:

api.py is where my dataclass was defined:

import dataclasses
import Optional

@dataclass
class user_api:
   id: str
   account: str
   email: Optional[str] = ""
   phone: field(repr = False)
   active: field(repr = False)
   state: field(repr = False)

app.py

import api
import requests

response = ('some code to authenticate the api and get data').json()

response_data = [getattr(api, appsflyer_endpoint)(**i) for i in response[appsflyer_endpoint]] 
#Here api is imported from above dataclass

I'm getting the below result when I print the response_data. I don't want to change the output format because this format is being used across different applications.

I just want to extract some specific fields from the final response

    [User(id = 1, account = 234, email='[email protected]', 'phone':{'sms':2, 'email':2}, active: true),
     User(id = 2, account = 567, email='[email protected]', 'phone':{'sms':1, 'email':1}, active: true, state: 'NY'),
     User(id = 3, account = 890, email='[email protected]', 'phone':{'sms':2, 'email':2}, active: true)
    ]

How can I extract the only particular fields from the above data i.e. id, account and email

The final output I'm expecting was

[User(id = 1, account = 234, email='[email protected]'),
 User(id = 2, account = 567, email='[email protected]'),
 User(id = 3, account = 890, email='[email protected]')]

I tried to extract its data from a tuple but didn't work. I'm not sure about this type of data format

Upvotes: 0

Views: 107

Answers (1)

bdempe
bdempe

Reputation: 317

One option is to exclude fields from the string representation of the User dataclass. That way the object will still have the "extra" attributes in the background. For example,

@dataclass
class user_api:
   id: str
   account: str
   email: str
   phone: str = field(repr=False)
   active: str = field(repr=False)
   state: str = field(repr=False)

Upvotes: 2

Related Questions