'Reading a ({...}) file in python [closed]

I am carrying out an api call in python for paper trading command --> api.get_account() and getting below output just for example. Is there any easy way to read this string and use the dictionary or json way ?

Account({   'account_blocked': False,
    'account_number': 'XXX',
    'accrued_fees': '0',
    'buying_power': '158636.2056',
    'cash': '79007.6228',
    'created_at': '2022-02-04T18:16:52.455679Z',
    'crypto_status': 'ACTIVE',
    'currency': 'USD',
    'daytrade_count': 0,
    'daytrading_buying_power': '0',
    'equity': '99907.10448',
    'id': '818445eb-79e9-4861-86b3-ce3418a541cc',
    'initial_margin': '20589.00168',
    'last_equity': '100000',
    'last_maintenance_margin': '0',
    'long_market_value': '20899.48168',
    'maintenance_margin': '20464.80968',
    'multiplier': '2',
    'non_marginable_buying_power': '79007.62',
    'pattern_day_trader': False,
    'pending_transfer_in': '0',
    'portfolio_value': '99907.10448',
    'regt_buying_power': '158636.2056',
    'short_market_value': '0',
    'shorting_enabled': True,
    'sma': '0',
    'status': 'ACTIVE',
    'trade_suspended_by_user': False,
    'trading_blocked': False,
    'transfers_blocked': False}) 

I am using a complicated way of reading like

st = str(acc).split('(')[1].split(')')[0].replace('\n','').replace("'",'"').replace('False','"F"').replace('True','"T"')

and then converging to

res = json.loads(st)


Solution 1:[1]

my guess is you are using something like the alpac_trade_api, and what seeing looks like a __str__ output from using print method of object from call api.get_account(). if so there is no need to parse dict or json, the library is doing so. you should just have to do stuff like

account = api.get_account()
account.account_blocked
# or perhaps account["account_blocked"] depending on implementation

in short the library is converting to python obejcts for you. if still need dict you will have to look at internals of package and if stores the parsed dict in account object. though if they are not providing method you will be using internals that library users do not intend users to use so do at own risk

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kamster