I am working in Django and I am saving the below variable as a list:
manifestData = form.cleaned_data
so if I print this it returns:
[{'ProductCode': <Product: APPLES-1>, 'UnitQty': u'11', 'Price': u'11.00', 'Amount': u'121', 'DescriptionOfGoods': u'Washington Extra Fancy', 'Type': u'Cases', u'id': None, u'DELETE': False}, {'ProductCode': <Product: ORANGES-1>, 'UnitQty': u'1', 'Price': u'12.00', 'Amount': u'12', 'DescriptionOfGoods': u'SUNKIST ORANGES', 'Type': u'Cases', u'id': None, u'DELETE': False}]
I need to pull the ProductCode values out of this and save them to variables. Really I need the values APPLES-1 and ORANGES-1 pulled from the list. What would my best approach be for this?
Thanks!
so you need to use map function:-
map(lambda x: x['ProductCode'], manifestData)
Can you share your Product model? Assuming, the model has a field as name you could try
codes = map(lambda x: x['ProductCode'].name, manifestData)
Related
I have this nested json dictionary and I want to rename the key name 'Keys' with its equivalent value using Python. I wonder if this is possible?
Current - 'Keys': ['AWS Backup']
I want it to be - 'AWS Backup': ['AWS Backup']
Sample json dictionary
{'TimePeriod': {'Start': '2022-11-28', 'End': '2022-11-29'}, 'Total': {}, 'Groups': [{'Keys': ['AWS Backup'], 'Metrics': {'UnblendedCost': {'Amount': '0.000000111', 'Unit': 'USD'}}}, {'Keys': ['AWS Direct Connect'], 'Metrics': {'UnblendedCost': {'Amount': '0.0000111', 'Unit': 'USD'}}}, {'Keys': ['AWS Key Management Service'], 'Metrics': {'UnblendedCost': {'Amount': '0.000000111', 'Unit': 'USD'}}}
Tried do flatten the json but after doing it still no luck. I'm not sure if I can do it using pandas dataframe also? Plan to save that json also in a csv file.
I think it is best if you follow the same method as the answer here, you cannot rename it, but you can copy the data to a new key and get rid of the old one
d = {'old_name': 1}
d['new_name'] = d.pop('old_name')
Python Edit/Rename Key Names in .json
Working on a little project and I may be in over my head. Using the CoinMarketCap API, I am trying to understand how to parse through their results to pull out just specific pieces of the returned value.
As an example:
result = {'status': {'timestamp': '2021-02-22T00:04:51.978Z', 'error_code': 0, 'error_message': None, 'elapsed': 46, 'credit_count': 1, 'notice': None}, 'data': {'1INCH': {'id': 8104, 'name': '1inch', 'symbol': '1INCH', 'slug': '1inch', 'cmc_rank': 83, 'last_updated': '2021-02-22T00:03:09.000Z', 'quote': {'BTC': {'price': 8.793673178965842e-05, 'volume_24h': 4010.9008604493424, 'percent_change_1h': 1.77689058, 'percent_change_24h': -3.76351861, 'percent_change_7d': -19.9798068, 'percent_change_30d': 66.4333541, 'market_cap': 12615.667000751586, 'last_updated': '2021-02-22T00:03:02.000Z'}}}}}
I am unable to figure out how to extract the 'symbol', 'cmc_rank', and 'market_cap' values from this variable. What is the proper approach to doing so?
Thank you
Try this solution, it should give you what you're looking for:
symbol = result['data']['1INCH']['symbol']
cmc_rank = result['data']['1INCH']['cmc_rank']
market_cap = result['data']['1INCH']['quote']['BTC']['market_cap']
Did you try this?
print(result["data"]["1INCH"]["symbol"])
print(result["data"]["1INCH"]["cmc_rank"])
print(result["data"]["1INCH"]["quote"]["BTC"]["market_cap"])
The "{}" indicate a python dict (dictionary) -- (square brackets [] would be a list, and parentheses () would be a tuple). Dicts can also be nested (same with lists and tuples)
In this case, you have a nested dictionar(ies). It's helpful to do some indentation...
result = {
'status': {
'timestamp': '2021-02-22T00:04:51.978Z',
'error_code': 0,
'error_message': None, 'elapsed': 46, 'credit_count': 1, 'notice': None},
'data': {
'1INCH': {
'id': 8104, 'name': '1inch', 'symbol': '1INCH', 'slug': '1inch', 'cmc_rank': 83, 'last_updated': '2021-02-22T00:03:09.000Z',
'quote': {
'BTC': {
'price': 8.793673178965842e-05, 'volume_24h': 4010.9008604493424, 'percent_change_1h': 1.77689058, 'percent_change_24h': -3.76351861, 'percent_change_7d': -19.9798068, 'percent_change_30d': 66.4333541, 'market_cap': 12615.667000751586, 'last_updated': '2021-02-22T00:03:02.000Z'}}}}}
The part to the left of the colon (:) in a dict is the key and the part to the right is the value.
So in the example you gave: result['data']['1INCH']['symbol'] would give you the value of symbol and result['data']['1INCH']['quote']['BTC']['market_cap'] would give you the value of market cap.
HOWEVER, this will only work if the keys don't change. In this case, it looks like the result is coming back with the symbol ('1INCH') as the key. Same with the currency ('BTC'). If you are always expecting '1INCH' and 'BTC' then you can hard code it. On the other hand, if the symbol and/or currency changes you would want to (a) store variables and use those instead (e.g. symbol='1INCH' .... result = x.query(symbol).... result['data'][symbol].....) OR (2) get the keys or (3) loop.
To get a list of the keys of any dictionary -- in this example, the keys of the dictionary 'data' : dkeys = list(result['data'].keys()) ... you can then check the length with len(dkeys) and/or access the key with numbers (since it's a list) dkeys[0]. So, something like result['data'][dkeys[0]]...
Or you could loop - which would be great if you have multiple results:
#the .items() method will return 2 values - the key and value for each entry
for k, v in result['data'].items():
#k would be the symbol in this case and v is the dictionary represented by that key
market_cap = v['quote']['BTC']['market_cap']
#note if there are multiple symbols here, it would overwrite market_cap...
I'm trying to get some parameters of my orders from Python-Binance API.
In this case, I would like to get only the 'status' of an order and save it into a variable.
I used this to get my last order :
orders = client.get_all_orders(symbol=symbol, limit=1)
And I get this result:
[{'clientOrderId': 'HzHxjogJf5rXrzD2uFnTyMn',
'cummulativeQuoteQty': '12.06757100',
'executedQty': '0.00030000',
'icebergQty': '0.00000000',
'isWorking': True,
'orderId': 88978639302,
'orderListId': -1,
'origQty': '0.00030000',
'origQuoteOrderQty': '0.00000000',
'price': '31558.57000000',
'side': 'BUY',
'status': 'FILLED',
'stopPrice': '31592.06000000',
'symbol': 'BTCUSDT',
'time': 1612653434918,
'timeInForce': 'GTC',
'type': 'STOP_LOSS_LIMIT',
'updateTime': 1612109872451}]
Tried to print just the status:
pprint.pprint(orders['status'])
But it returns an error:
TypeError: list indices must be integers or slices, not str
Try this:
pprint.pprint(orders[0]['status'])
order variable is a list that contains dictionary. To get access to first element of the list use [0]. Now you only get value from dictionary. You can do this using [dictionary_key] in this example ['status']. Combining this you can print status.
If you are not sure which type is your variable use simply print(type(variable_name))
I am trying to figure out how to do an assertion to see if a number exists in a list.
So my list looks like:
data = [{'value': Decimal('4.21'), 'Type': 'sale'},
{'value': Decimal('84.73'), 'Type': 'sale'},
{'value': Decimal('70.62'), 'Type': 'sale'},
{'value': Decimal('15.00'), 'Type': 'credit'},
{'value': Decimal('2.21'), 'Type': 'credit'},
{'value': Decimal('4.21'), 'Type': 'sale'},
{'value': Decimal('84.73'), 'Type': 'sale'},
{'value': Decimal('70.62'), 'Type': 'sale'},
{'value': Decimal('15.00'), 'Type': 'credit'},
{'value': Decimal('2.21'), 'Type': 'credit'}]
Now I am trying to iterate through the list like:
for i in data:
s = i['value']
print(s)
assert 2.21 in i['value'], "Value should be there"
I am somehow only getting the first number returned for "value" i.e. 4.21
You have two problems as other commenters pointed out. You compare the wrong data types (str against Decimal, or after your edit, float against Decimal) and you also terminate on first failure. You probably wanted to write:
assert Decimal('2.21') in (d["value"] for d in data)
This will extract the value of the "value" key from each sub-dictionary inside the list and search for Decimal('2.21') in them.
I have 2 lists of dicts, both with further nesting in them, the first list of dicts called original_state below is (as it suggests) the initial state received when the server is switched on.
A script executes (which is how the initial state is obtained) and continues to look for updates. These updates come through periodically in the form of updated_orders below (this all functions correctly).
I am trying (and failing miserably) to try and write a function that looks at the list of dicts updated_orders and then adds those orders into the correct place within original_state. The correct place is determined by the 'selection_id' and the 'market_id'.
Things to consider:
1) There could be selection_id's in updated_orders that do not exist in original_state (the reverse will be true most of the time as orders are only returned when they come in, hence most selection_id's in original_state will not have a corresponding value in updated_orders, but this is less important as updated_orders does not need to be updated).
2) When a selection_id is in updated_orders and not in original_state a new entry should be added, when a selection_id is in both then any new order_id's should be inserted and any existing ones overwritten if different.
3) The above also applies for Market_id's (which adds a whole new level of complexity, so please feel free to ignore this for now as this is a relatively unlikely scenario that I can deal with down the line and/or by another method).
3) An order_id is totally unique
4) A market_id is totally unique
5) A selection_id could occur in more than one market
6) If it helps, market_id is an available field to use within live_orders next to 'selection_id' (it is omitted in the example data set I've given)
I think that's all the logic, hopefully I've explained it clearly, but please feel free to ask for clarification on anything.
Now the embarrassing bit...I would normally post an extensive code snippet for people to critique, however here I really don't even know where to begin, so although I have tried plenty of ways my syntax has never been valid, hence I have made virtually no progress.
I obviously don't expect anyone to write everything for me, so if someone is kind enough to give me a pointer I can then hopefully complete the function and post the final code to help anyone else with a similar issue.
original_state = [{'Market_id': '1.130856098', 'Market_name': 'Market A', 'position_list':
[{'Market_id': '1.130856098', 'Selection_id': 12832765, 'Last_price': 4.1,
'Orders':
[{'order_id': '163785114305', 'price': 100, 'size': 5.0},
{'order_id': '173785114311', 'price': 99, 'size': 5.0}], 'Status': 'ACTIVE'},
{'Market_id': '1.130856099', 'Selection_id': 12832767, 'Last_price': 4.1,
'Orders':
[{'order_id': '163785114305', 'price': 100, 'size': 5.0},
{'order_id': '173785114311', 'price': 99, 'size': 5.0}], 'Status': 'ACTIVE'}]},
{'Market_id': '1.130856099', 'Market_name': 'Market B', 'position_list':
[{'Market_id': '1.130856099', 'Selection_id': 12832765, 'Last_price': 4.1,
'Orders':
[{'order_id': '163785114305', 'price': 100, 'size': 5.0},
{'order_id': '173785114311', 'price': 99, 'size': 5.0}], 'Status': 'ACTIVE'},
{'Market_id': '1.130856099', 'Selection_id': 12832763, 'Last_price': 4.1,
'Orders':
[{'order_id': '163785114305', 'price': 100, 'size': 5.0},
{'order_id': '173785114311', 'price': 99, 'size': 5.0}],
'Status': 'ACTIVE'}]}]
updated_orders=[{'Last_update': 'Mon May 20 18:23:38 2019', 'Market_id': '1.130856098', 'Market_name': 'Market A',
'Live_orders':
[{'selection_id': 12832765, 'live_orders':
[{'order_id': '165150568348', 'price': 55.5, 'size': 3},
{'order_id': '165147033010', 'price': 46, 'size': 12}]},
{'selection_id': 12832767, 'live_orders':
[{'order_id': '165150568333', 'price': 32, 'size': 3.5},
{'order_id': '165147033055', 'price': 67, 'size': 7.8}]}]},
{'Last_update': 'Mon May 20 18:23:38 2019', 'Market_id': '1.130856099', 'Market_name': 'Market B',
'Live_orders':
[{'selection_id': 12832765, 'live_orders':
[{'order_id': '165150568348', 'price': 55.5, 'size': 54},
{'order_id': '165147033010', 'price': 46, 'size': 9}]},
{'selection_id': 12832766, 'live_orders':
[{'order_id': '165150568349', 'price': 56, 'size': 6},
{'order_id': '165147033011', 'price': 47, 'size': 32}]}]}]}]
new_order_list = []
for x in updated_orders:
for y in x['Live_orders']:
orders_to_update = {}
orders_to_update.update({'Market_id': x['Market_id'],
'Selection_id': y['selection_id'],
'live_orders': y['live_orders']})
new_order_list.append(orders_to_update)
for z in new_order_list:
for a in original_state:
for b in a['position_list']:
if z['Market_id'] == b['Market_id'] and z['Selection_id'] == b['Selection_id']:
print(z['Market_id'], z['Selection_id'])
I wonder if I've been massively over complicating this, the above appears to filter in the way (I think) I require, so then the next step would be to update the original_state with updated_orders based on the above selections.
I may have been massively over complicating things here, this appears to do what I want (ignoring the insertion of completely new markets / selection_id's for now):
new_order_list = []
for x in updated_orders:
for y in x['Live_orders']:
orders_to_update = {}
orders_to_update.update({'Market_id': x['Market_id'],
'Selection_id': y['selection_id'],
'live_orders': y['live_orders']})
new_order_list.append(orders_to_update)
for z in new_order_list:
for a in original_state:
for b in a['position_list']:
if z['Market_id'] == b['Market_id'] and z['Selection_id'] == b['Selection_id']:
#for c in b['Orders']:
print(b['Market_id'], b['Selection_id'], z['live_orders'])
b.update({'Orders': z['live_orders']})
I must have missed the obvious solution and was looking for something far too complex, but hopefully this will help someone else as this must be a fairly common problem (yet with very few posts on the solution)!
Here is how I tackled the updates where the orders were not in the original_state:
uo_list = []
for i in updated_orders:
uo_list.append(i['Market_id'])
os_list = []
for j in original_state:
os_list.append(j['Market_id'])
new_list = list(set(uo_list).difference(os_list))
for i in updated_orders:
for j in new_list:
if i['Market_id'] == j:
print('Append this as required', i)