i just started up using phython & i try to help my dad to develop a small program:
from a query i get a retun that i converted into a dict as i want to extract the 'last_price': '1704.50'. I tried it with find, count ... but it seems that i'm running into a wall.
here is the string & it looks like that the bracket makes it harder to extract:
dict_items([('ret_code', 0), ('ret_msg', 'OK'), ('ext_code', ''), ('ext_info', ''), ('result', [{'symbol': 'ETHUSDT', 'bid_price': '1701.8', 'ask_price': '1702', 'last_price': '1704.50', 'last_tick_direction': 'PlusTick', 'prev_price_24h': '1647.55', 'price_24h_pcnt': '0.034566', 'high_price_24h': '1710.55', 'low_price_24h': '1620.40', 'prev_price_1h': '1679.40', 'price_1h_pcnt': '0.014945', 'mark_price': '1702.51', 'index_price': '1702.38', 'open_interest': 150155.81, 'open_value': '0.00', 'total_turnover': '54418340521.33', 'turnover_24h': '63239638.53', 'total_volume': 58525803.73, 'volume_24h': 37976.79, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2022-08-25T08:00:00Z', 'countdown_hour': 1, 'delivery_fee_rate': '', 'predicted_delivery_price': '', 'delivery_time': ''}]), ('time_now', '1661411154.330653')])
Any idea is more than welcome
Thanks
Alice
Hope I'm uderstanding it right but this works for me:
d=dict([('ret_code', 0), ('ret_msg', 'OK'), ('ext_code', ''), ('ext_info', ''), ('result', [{'symbol': 'ETHUSDT', 'bid_price': '1701.8', 'ask_price': '1702', 'last_price': '1704.50', 'last_tick_direction': 'PlusTick', 'prev_price_24h': '1647.55', 'price_24h_pcnt': '0.034566', 'high_price_24h': '1710.55', 'low_price_24h': '1620.40', 'prev_price_1h': '1679.40', 'price_1h_pcnt': '0.014945', 'mark_price': '1702.51', 'index_price': '1702.38', 'open_interest': 150155.81, 'open_value': '0.00', 'total_turnover': '54418340521.33', 'turnover_24h': '63239638.53', 'total_volume': 58525803.73, 'volume_24h': 37976.79, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2022-08-25T08:00:00Z', 'countdown_hour': 1, 'delivery_fee_rate': '', 'predicted_delivery_price': '', 'delivery_time': ''}]), ('time_now', '1661411154.330653')])
d['result'][0]['last_price']
gives the number. Notice the [0] due to the "[ ]" you mentioned.
If you are dealing with texts, regex might be more appropriate:
import re
text = """dict_items([('ret_code', 0), ('ret_msg', 'OK'), ('ext_code', ''), ('ext_info', ''), ('result', [{'symbol': 'ETHUSDT', 'bid_price': '1701.8', 'ask_price': '1702', 'last_price': '1704.50', 'last_tick_direction': 'PlusTick', 'prev_price_24h': '1647.55', 'price_24h_pcnt': '0.034566', 'high_price_24h': '1710.55', 'low_price_24h': '1620.40', 'prev_price_1h': '1679.40', 'price_1h_pcnt': '0.014945', 'mark_price': '1702.51', 'index_price': '1702.38', 'open_interest': 150155.81, 'open_value': '0.00', 'total_turnover': '54418340521.33', 'turnover_24h': '63239638.53', 'total_volume': 58525803.73, 'volume_24h': 37976.79, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2022-08-25T08:00:00Z', 'countdown_hour': 1, 'delivery_fee_rate': '', 'predicted_delivery_price': '', 'delivery_time': ''}]), ('time_now', '1661411154.330653')])"""
last_price_pattern = r"'last_price'\s*:\s*'(\d*\.\d*)'"
last_price = float(re.findall(last_price_pattern, text)[0])
print(last_price)
If you're dealing with a list, then simple indexing will suffice:
raw_data = [('ret_code', 0), ('ret_msg', 'OK'), ('ext_code', ''), ('ext_info', ''), ('result', [{'symbol': 'ETHUSDT', 'bid_price': '1701.8', 'ask_price': '1702', 'last_price': '1704.50', 'last_tick_direction': 'PlusTick', 'prev_price_24h': '1647.55', 'price_24h_pcnt': '0.034566', 'high_price_24h': '1710.55', 'low_price_24h': '1620.40', 'prev_price_1h': '1679.40', 'price_1h_pcnt': '0.014945', 'mark_price': '1702.51', 'index_price': '1702.38', 'open_interest': 150155.81, 'open_value': '0.00', 'total_turnover': '54418340521.33', 'turnover_24h': '63239638.53', 'total_volume': 58525803.73, 'volume_24h': 37976.79, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2022-08-25T08:00:00Z', 'countdown_hour': 1, 'delivery_fee_rate': '', 'predicted_delivery_price': '', 'delivery_time': ''}]), ('time_now', '1661411154.330653')]
last_price = float(raw_data[4][1][0]['last_price'])
print(last_price)
Related
Current dictionary is [[{ data }]]
[[{'stat': 'Ok', 'time': '22-12-2022 15:25:00', 'ssboe': '1671702900', 'into': '332.40', 'inth': '332.80', 'intl': '332.00', 'intc': '332.45', 'intvwap': '332.30', 'intv': '140828', 'intoi': '0', 'v': '6583955', 'oi': '0'}, {'stat': 'Ok', 'time': '22-12-2022 15:20:00', 'ssboe': '1671702600', 'into': '332.30', 'inth': '332.45', 'intl': '332.25', 'intc': '332.35', 'intvwap': '332.23', 'intv': '117775', 'intoi': '0', 'v': '6443127', 'oi': '0'}]]
Expected dictionary is [{ data }]
[{'stat': 'Ok', 'time': '22-12-2022 15:25:00', 'ssboe': '1671702900', 'into': '332.40', 'inth': '332.80', 'intl': '332.00', 'intc': '332.45', 'intvwap': '332.30', 'intv': '140828', 'intoi': '0', 'v': '6583955', 'oi': '0'}, {'stat': 'Ok', 'time': '22-12-2022 15:20:00', 'ssboe': '1671702600', 'into': '332.30', 'inth': '332.45', 'intl': '332.25', 'intc': '332.35', 'intvwap': '332.23', 'intv': '117775', 'intoi': '0', 'v': '6443127', 'oi': '0'}]
Just get the first index like so:
new_list_of_data = old_list_of_data[0]
I am trying to convert a list into JSON using Python. The result from a query looks something like this :
Output:
[<Record r=<Relationship id=106 nodes=(<Node id=71 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '105', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'remove', 'sing': '', 'pl': '', 'title': 'BMProcessor', 'version': 'v6.0'}>, <Node id=59 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '93', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'A DataProcessor which represents a ML algorithm', 'sing': '', 'pl': '', 'title': 'LearningProcessor', 'version': 'v6.0'}>) type='subclass_of' properties={}> b=<Node id=59 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '93', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'A DataProcessor which represents a ML algorithm', 'sing': '', 'pl': '', 'title': 'LearningProcessor', 'version': 'v6.0'}> n=<Node id=71 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '105', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'remove', 'sing': '', 'pl': '', 'title': 'BMProcessor', 'version': 'v6.0'}>>]
Function :
def runQuery(query):
pprint.pprint(connection.execute(query))
When I perform a simple json.dumps() it return with TypeError: Object of type is not JSON serializable
I want to print a JSON format out of this. How can I do so?
You can get the result.data() which is a dictionary.
You can also iterate over the records in the cursor and convert one by one, and extract the fields you want, which is what the linked example above does - pass the cursor and use the blob['field'] syntax
def serialize_genre(genre):
return {
'id': genre['id'],
'name': genre['name'],
}
Building off my former post here: How to print data from API call into a CSV file
The API call returns this
[Order({ 'asset_class': 'us_equity',
'asset_id': '8a9-43b6-9b36-662f01e8fadd',
'canceled_at': None,
'client_order_id': 'e38a-b51c-349314bc6e9e',
'created_at': '2020-06-05T16:16:53.307491Z',
'expired_at': None,
'extended_hours': False,
'failed_at': None,
'filled_at': '2020-06-05T16:16:53.329Z',
'filled_avg_price': '7.8701',
'filled_qty': '45',
'id': '8-4888-9c7c-97bf8c2a3a16',
'legs': None,
'limit_price': '7.87',
'order_class': '',
'order_type': 'limit',
'qty': '45',
'replaced_at': None,
'replaced_by': None,
'replaces': None,
'side': 'sell',
'status': 'filled',
'stop_price': None,
'submitted_at': '2020-06-05T16:16:53.293859Z',
'symbol': 'CARS',
'time_in_force': 'day',
'type': 'limit',
'updated_at': '2020-06-08T11:21:51.411547Z'}), Order({ 'asset_class': 'us_equity',
'asset_id': '1aef-42f4-9975-750dbcb3e67d',
'canceled_at': None,
'client_order_id': '2bde-4572-a5d0-bfc32c2bf31a',
'created_at': '2020-06-05T16:16:37.508176Z',
'expired_at': None,
'extended_hours': False,
'failed_at': None,
'filled_at': '2020-06-05T16:16:37.531Z',
'filled_avg_price': '10.8501',
'filled_qty': '26',
'id': '4256-472c-a5de-6ca9d6a21422',
'legs': None,
'limit_price': '10.85',
'order_class': '',
'order_type': 'limit',
'qty': '26',
'replaced_at': None,
'replaced_by': None,
'replaces': None,
'side': 'sell',
'status': 'filled',
'stop_price': None,
'submitted_at': '2020-06-05T16:16:37.494389Z',
'symbol': 'IGT',
'time_in_force': 'day',
'type': 'limit',
'updated_at': '2020-06-08T11:21:51.424963Z'})]
I'd like to repeat the exercise of writing to a CSV as linked in my other post but this time only write a subset of the columns from the API into a CSV. My first attempt here was instead of using the keys from the raw dictionary I would specify the fieldnames as a list, but then I'm having trouble accessing only the keys in the dict entry based on the list of filenames that I'm passing in.
with open('historical_orders.csv', 'w', newline='') as csvfile:
fieldnames = ['id', 'created_at', 'filled_at', 'canceled_at', 'replaced_at', 'symbol', 'asset_class', 'qty',
'filled_qty', 'filled_avg_price', 'order_class', 'order_type', 'type',
'side', 'time_in_force', 'limit_price',
'stop_price', 'status', 'extended_hours', 'legs']
writer = csv.DictWriter(csvfile, fieldnames)
writer.writeheader()
for order in closed_orders:
writer.writerow(order.__dict__['_raw'].fieldnames)
I get AttributeError: 'dict' object has no attribute 'fieldnames'.
Additionally I'd like to add 1 more column that strips out the funky "created_at" value to a date and time. So instead of "created_at" = "'2020-06-05T16:16:53.307491Z'", I'd like to create a column date "'2020-06-05" and time "'16:16:53". I was thinking I could do this by adding a loop in each write row to write one field a time, but wasn't sure if there was a better way.
Can someone help me with these 2 issues?
I'm using the Alpaca trading API and want to export data from a function call into a CSV file.
When I run a call like this:
closed_orders = api.list_orders(
status='closed',
limit=2,
nested=True # show nested multi-leg orders
)
print(closed_orders)
I get back:
[Order({ 'asset_class': 'us_equity',
'asset_id': '8a9-43b6-9b36-662f01e8fadd',
'canceled_at': None,
'client_order_id': 'e38a-b51c-349314bc6e9e',
'created_at': '2020-06-05T16:16:53.307491Z',
'expired_at': None,
'extended_hours': False,
'failed_at': None,
'filled_at': '2020-06-05T16:16:53.329Z',
'filled_avg_price': '7.8701',
'filled_qty': '45',
'id': '8-4888-9c7c-97bf8c2a3a16',
'legs': None,
'limit_price': '7.87',
'order_class': '',
'order_type': 'limit',
'qty': '45',
'replaced_at': None,
'replaced_by': None,
'replaces': None,
'side': 'sell',
'status': 'filled',
'stop_price': None,
'submitted_at': '2020-06-05T16:16:53.293859Z',
'symbol': 'CARS',
'time_in_force': 'day',
'type': 'limit',
'updated_at': '2020-06-08T11:21:51.411547Z'}), Order({ 'asset_class': 'us_equity',
'asset_id': '1aef-42f4-9975-750dbcb3e67d',
'canceled_at': None,
'client_order_id': '2bde-4572-a5d0-bfc32c2bf31a',
'created_at': '2020-06-05T16:16:37.508176Z',
'expired_at': None,
'extended_hours': False,
'failed_at': None,
'filled_at': '2020-06-05T16:16:37.531Z',
'filled_avg_price': '10.8501',
'filled_qty': '26',
'id': '4256-472c-a5de-6ca9d6a21422',
'legs': None,
'limit_price': '10.85',
'order_class': '',
'order_type': 'limit',
'qty': '26',
'replaced_at': None,
'replaced_by': None,
'replaces': None,
'side': 'sell',
'status': 'filled',
'stop_price': None,
'submitted_at': '2020-06-05T16:16:37.494389Z',
'symbol': 'IGT',
'time_in_force': 'day',
'type': 'limit',
'updated_at': '2020-06-08T11:21:51.424963Z'})]
How do I grab this data and print it into a CSV? I tried to write something like this but I get the error "Order" object has no keys. I assumed I'd be able to loop through the API response and write according to the headers; how do I break down/flatten the API response accordingly?
with open('historical_orders.csv', 'w', newline='') as csvfile:
fieldnames = [
'asset_id',
'canceled_at',
'client_order_id',
'created_at',
'expired_at',
'extended_hours',
'failed_at',
'filled_at',
'filled_avg_price',
'filled_qty',
'id',
'legs',
'limit_price',
'order_class',
'order_type',
'qty',
'replaced_at',
'replaced_by',
'replaces',
'side',
'status',
'stop_price',
'submitted_at',
'symbol',
'time_in_force',
'type',
'updated_at'
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for c in closed_orders:
writer.writerow(c)
You can access the __dict__ attribute of the Orders class to get the headers and rows for the CSV file.
with open('historical_orders.csv', 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=closed_orders[0].__dict__['_raw'].keys())
writer.writeheader()
for order in closed_orders:
writer.writerow(order.__dict__['_raw'])
I have a big python dictionary. One of its keys has another dictionary as the value. I want to create a new dictionary using the values and then delete the key from my original dictionary.
Is there any function to export the values into another dictionary? To delete I know I can use the .pop() function. I tried googling but wasn't successful.
This is the dictionary. I used stars to block out sensitive information. The key I need is billing_address, which has another dictionary as the values:
{
'shipping_cost_tax': '0.0000',
'refunded_amount': '0.0000',
'external_source': None,
'discount_amount': '0.0000',
'base_wrapping_cost': '0.0000',
'shipping_cost_tax_class_id': 2,
'payment_method': 'PayPal',
'handling_cost_ex_tax': '0.0000',
'store_credit_amount': '0.0000',
'shipping_cost_inc_tax': '11.0000',
'handling_cost_tax_class_id': 2,
'currency_id': 1,
'payment_status': 'captured',
'subtotal_ex_tax': '99.0000',
'total_inc_tax': '11.0000',
'handling_cost_inc_tax': '0.0000',
'total_ex_tax': '11.0000',
'is_deleted': False,
'status_id': 5,
'id': 614534,
'shipping_cost_ex_tax': '11.0000',
'date_shipped': '',
'order_source': 'www',
'status': 'Cancelled',
'handling_cost_tax': '0.0000',
'items_total': 3,
'wrapping_cost_tax': '0.0000',
'date_created': 'Wed,
09 Jul 2014 12:22:17 +0000',
'total_tax': '0.0000',
'order_is_digital': False,
'date_modified': 'Thu,
30 Oct 2014 02:34:07 +0000',
'geoip_country': 'Australia',
'base_shipping_cost': '11.0000',
'payment_provider_id': '**************',
'staff_notes': '',
'default_currency_id': 1,
'currency_code': 'AUD',
'currency_exchange_rate': '1.0000000000',
'coupon_discount': '99.0000',
'customer_message': '',
'subtotal_inc_tax': '99.0000',
'gift_certificate_amount': '0.0000',
'items_shipped': 0,
'default_currency_code': 'AUD',
'customer_id': 1,
'geoip_country_iso2': 'AU',
'ip_address': '124.168.160.136',
'shipping_address_count': 1,
'wrapping_cost_ex_tax': '0.0000',
'base_handling_cost': '0.0000',
'wrapping_cost_tax_class_id': 3,
'ebay_order_id': '0',
'wrapping_cost_inc_tax': '0.0000',
'billing_address': {
'state': '*******',
'street_1': '*************',
'street_2': '',
'country_iso2': 'AU',
'last_name': '************',
'company': '***************',
'country': 'Australia',
'first_name': '*********',
'email': '***************',
'phone': '*************',
'city': '*************',
'zip': '************'
},
'subtotal_tax': '0.0000'
}
EDIT:
def popAndMergeDicts(line):
tempDict = line['billing_address']
del line['billing_address']
for i in tempDict:
line[i] = tempDict[i]
print(line)
def process_file(filename):
lines = tuple(open(filename))
for line in lines[0:1]:
popAndMergeDicts(line)
process_file('allOrdersData')
allOrdersData is a file where I have many dictionaries like the one I posted before, one in each line. I get the following error when I try to run it:
TypeError: string indices must be integers
EDIT 2:
Nevermind, got it working with ast.literal_eval:
import ast
def popAndMergeDicts(line):
dictLine = ast.literal_eval(line)
tempDict = dictLine['billing_address']
del dictLine['billing_address']
for i in tempDict:
dictLine[i] = tempDict[i]
print(dictLine)
def process_file(filename):
lines = tuple(open(filename))
for line in lines[0:]:
popAndMergeDicts(line)
process_file('allOrdersData')
Just use .pop(): it returns the value it pops. Eg,
#!/usr/bin/env python
import pprint
big_dict = {
'shipping_cost_tax': '0.0000',
'refunded_amount': '0.0000',
#etc
'billing_address': {
'state': '*******',
'street_1': '*************',
'street_2': '',
'country_iso2': 'AU',
#etc
},
'subtotal_tax': '0.0000'
}
print 'Before'
pprint.pprint(big_dict, indent=4)
bill_dict = big_dict.pop('billing_address')
print '\nBill dict'
pprint.pprint(bill_dict, indent=4)
print '\nAfter'
pprint.pprint(big_dict, indent=4)
output
Before
{ 'billing_address': { 'country_iso2': 'AU',
'state': '*******',
'street_1': '*************',
'street_2': ''},
'refunded_amount': '0.0000',
'shipping_cost_tax': '0.0000',
'subtotal_tax': '0.0000'}
Bill dict
{ 'country_iso2': 'AU',
'state': '*******',
'street_1': '*************',
'street_2': ''}
After
{ 'refunded_amount': '0.0000',
'shipping_cost_tax': '0.0000',
'subtotal_tax': '0.0000'}
To keep the keys/values in the original dictionary, instead of creating a new one, you can do what Marichyasana suggests:
bill_dict = big_dict.pop('billing_address')
for k in bill_dict:
big_dict[k] = bill_dict[k]
del bill_dict
print '\nAfter'
pprint.pprint(big_dict, indent=4)
output
After
{ 'country_iso2': 'AU',
'refunded_amount': '0.0000',
'shipping_cost_tax': '0.0000',
'state': '*******',
'street_1': '*************',
'street_2': '',
'subtotal_tax': '0.0000'}
I've also deleted the temporary bill_dict. That's not strictly necessary, as bill_dict will automatically be deleted once it goes out of scope.
If you just want to reference the "sub-dictionary", you could simply use (where d is your original dictionary):
billing1 = d['billing_address']
If you want a distinct, separate copy, you could use any of the following:
billing2 = dict(d['billing_address'])
billing3 = d['billing_address'].copy()
billing4 = copy.copy(d['billing_address']) # after import copy
Changes to values in billing1 will be reflected in your original dictionary. Changes to values in the other three will not be reflected in your original dictionary.
If you want to delete the billing_address key also (like you suggest in your question but backpedal on in your comments), then use one of the above approaches first, followed by:
del d['billing_address']
Or, as one step, if you're committed to deleting the key from your original dictionary, use dict.pop()
billing5 = d.pop('billing_address')
Let 'a' be the name of the dictionary
Let 'b' be the name of the billing_address dictionary
b=a['billing_address']
del a['billing_address']
for i in b:
a[i]=b[i]
ans = {key:val for key, val in input_dict.pop('billing_address').items()}
dict1 = {'shipping_cost_tax': '0.0000', 'refunded_amount': '0.0000', 'external_source': None, 'discount_amount': '0.0000', 'base_wrapping_cost': '0.0000', 'shipping_cost_tax_class_id': 2, 'payment_method': 'PayPal', 'handling_cost_ex_tax': '0.0000', 'store_credit_amount': '0.0000', 'shipping_cost_inc_tax': '11.0000', 'handling_cost_tax_class_id': 2, 'currency_id': 1, 'payment_status': 'captured', 'subtotal_ex_tax': '99.0000', 'total_inc_tax': '11.0000', 'handling_cost_inc_tax': '0.0000', 'total_ex_tax': '11.0000', 'is_deleted': False, 'status_id': 5, 'id': 614534, 'shipping_cost_ex_tax': '11.0000', 'date_shipped': '', 'order_source': 'www', 'status': 'Cancelled', 'handling_cost_tax': '0.0000', 'items_total': 3, 'wrapping_cost_tax': '0.0000', 'date_created': 'Wed, 09 Jul 2014 12:22:17 +0000', 'total_tax': '0.0000', 'order_is_digital': False, 'date_modified': 'Thu, 30 Oct 2014 02:34:07 +0000', 'geoip_country': 'Australia', 'base_shipping_cost': '11.0000', 'payment_provider_id': '**************', 'staff_notes': '', 'default_currency_id': 1, 'currency_code': 'AUD', 'currency_exchange_rate': '1.0000000000', 'coupon_discount': '99.0000', 'customer_message': '', 'subtotal_inc_tax': '99.0000', 'gift_certificate_amount': '0.0000', 'items_shipped': 0, 'default_currency_code': 'AUD', 'customer_id': 1, 'geoip_country_iso2': 'AU', 'ip_address': '124.168.160.136', 'shipping_address_count': 1, 'wrapping_cost_ex_tax': '0.0000', 'base_handling_cost': '0.0000', 'wrapping_cost_tax_class_id': 3, 'ebay_order_id': '0', 'wrapping_cost_inc_tax': '0.0000', 'billing_address': {'state': '*******', 'street_1': '*************', 'street_2': '', 'country_iso2': 'AU', 'last_name': '************', 'company': '***************', 'country': 'Australia', 'first_name': '*********', 'email': '***************', 'phone': '*************', 'city': '*************', 'zip': '************'}, 'subtotal_tax': '0.0000'}
print {'billing_address': dict1['billing_address']}