ameliabob Posted July 6, 2018 Share Posted July 6, 2018 I am receiving a string and I can't see to get it into a dictionary.. For example I have a string: [{"rowId": 21,"nature": "O","symbol": "TAL","qty": 10,"daysHeld": 0,"source": "Momentum","dayEntered": "2018-01-16","pl": -40}, {"rowId": 22,"nature": "O","symbol": "SGMO","qty": 5,"daysHeld": 35,"source": "Lightning Trend","dayEntered": "2018-02-06","pl": 2300}, {"rowId": 23,"nature": "O","symbol": "NVDA","qty": 10,"daysHeld": 61,"source": "Tactical Trader","dayEntered": "2018-02-09","pl": -15300}, {"rowId": 24,"nature": "O","symbol": "ALGN","qty": 10,"daysHeld": 88,"source": "none","dayEntered": "2018-01-18","pl": -17100}, {"rowId": 25,"nature": "O","symbol": "IMGN","qty": 10,"daysHeld": 67,"source": "Lightning Trend","dayEntered": "2018-02-08","pl": 460}] How do I make this into a dict? How would I retreive the 'dayentered ' data from the line with symbol ALGN? Thanx Link to comment Share on other sites More sharing options...
justsomeguy Posted July 6, 2018 Share Posted July 6, 2018 Each entry needs a unique key, so decide what you want to use for the key for each entry. You can make each of those JSON objects a dictionary itself, and put all of them in another dictionary. Link to comment Share on other sites More sharing options...
dsonesuk Posted July 6, 2018 Share Posted July 6, 2018 needle='ALGN' whatever = [{"rowId": 21,"nature": "O","symbol": "TAL","qty": 10,"daysHeld": 0,"source": "Momentum","dayEntered": "2018-01-16","pl": -40}, {"rowId": 22,"nature": "O","symbol": "SGMO","qty": 5,"daysHeld": 35,"source": "Lightning Trend","dayEntered": "2018-02-06","pl": 2300}, {"rowId": 23,"nature": "O","symbol": "NVDA","qty": 10,"daysHeld": 61,"source": "Tactical Trader","dayEntered": "2018-02-09","pl": -15300}, {"rowId": 24,"nature": "O","symbol": "ALGN","qty": 10,"daysHeld": 88,"source": "none","dayEntered": "2018-01-18","pl": -17100}, {"rowId": 25,"nature": "O","symbol": "IMGN","qty": 10,"daysHeld": 67,"source": "Lightning Trend","dayEntered": "2018-02-08","pl": 460}] for row in whatever: if needle == row['symbol']: print ('Specific Item') print(row['dayEntered']) print ("All items from specific row") for key, value in row.items(): print ("key: ",key,", value: ", value) Link to comment Share on other sites More sharing options...
ameliabob Posted July 7, 2018 Author Share Posted July 7, 2018 Thanks for the demo program. It gets me over the starting point. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now