Extracting a key from a dictionary to another dictionary in Python - python

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']}

Related

Phython: How to extract a number

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)

get a part from a dictionary

i'm trying to get the pulse as an output for the given url using this code
from OTXv2 import OTXv2
from OTXv2 import IndicatorTypes
otx = OTXv2("my_key")
test=otx.get_indicator_details_full(IndicatorTypes.DOMAIN, "google.com")
and when i print test i become this output:
{'general': {'sections': ['general', 'geo', 'url_list', 'passive_dns', 'malware', 'whois', 'http_scans'], 'whois': 'http://whois.domaintools.com/google.com', 'alexa': 'http://www.alexa.com/siteinfo/google.com', 'indicator': 'google.com', 'type': 'domain', 'type_title': 'Domain', 'validation': [{'source': 'ad_network', 'message': 'Whitelisted ad network domain www-google-analytics.l.google.com', 'name': 'Whitelisted ad network domain'}, {'source': 'akamai', 'message': 'Akamai rank: #3', 'name': 'Akamai Popular Domain'}, {'source': 'alexa', 'message': 'Alexa rank: #1', 'name': 'Listed on Alexa'}, {'source': 'false_positive', 'message': 'Known False Positive', 'name': 'Known False Positive'}, {'source': 'majestic', 'message': 'Whitelisted domain google.com', 'name': 'Whitelisted domain'}, {'source': 'whitelist', 'message': 'Whitelisted domain google.com', 'name': 'Whitelisted domain'}], 'base_indicator': {'id': 12915, 'indicator': 'google.com', 'type': 'domain', 'title': '', 'description': '', 'content': '', 'access_type': 'public', 'access_reason': ''}, 'pulse_info': {'count': 0, 'pulses': [], 'references': [], 'related': {'alienvault': {'adversary': [], 'malware_families': [], 'industries': []}, 'other': {'adversary': [], 'malware_families': [], 'industries': []}}}, 'false_positive':...
i want to get only the part 'count': 0 in pulse_info
i tried using test.values() but it's like i have many dictionaries together
any idea how can i solve that?
Thank you
print(test["general"]["pulse_info"]["count"])

How to convert a record or list into JSON format using python?

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'],
}

From MongoDB convert from dictionary to row with Pandas

This is a test coming from MongoDB, I need to convert to MySQL. But! Sometimes there is more then one "agents", if that's the case I need each agent on their own row and that agent should have the same "display_name". For example Walter should have Gloria on one row and Barb on next and both have Walt Mosley under "display_name".
[{'name': 'Loomis, Gloria',
'primaryemail': 'gloria#gmail.com',
'primaryphone': '212-382-1121'},
{'name': 'Hogson, Barb',
'primaryemail': 'bho124#aol.com',
'primaryphone': ''}]
I've tried this but it just splits out the key/values.
a,b,c = [[d[e] for d in test] for e in sorted(test[0].keys())]
print(a,b,c)
This is the original JSON format:
{'_id': ObjectId('58e6ececafb08d6'),
'item_type': 'Contributor',
'role': 0,
'short_bio': 'Walter Mosley (b. 1952)',
'firebrand_id': 1588,
'display_name': 'Walter Mosley',
'first_name': 'Walter',
'last_name': 'Mosley',
'slug': 'walter-mosley',
'updated': datetime.datetime(2020, 1, 7, 8, 17, 11, 926000),
'image': 'https://s3.amazonaws.com/8588-book-contributor.jpg',
'social_media_name': '',
'social_media_link': '',
'website': '',
'agents': [{'name': 'Loomis, Gloria',
'primaryemail': 'gloria#gmail.com',
'primaryphone': '212-382-1121'},
{'name': 'Hogson, Barb',
'primaryemail': 'bho124#aol.com',
'primaryphone': ''}],
'estates': [],
'deleted': False}
If you've an array of dictionaries from your JSON file, try this :
JSON input :
inputJSON = [{'item_type': 'Contributor',
'role': 0,
'short_bio': 'Walter Mosley (b. 1952)',
'firebrand_id': 1588,
'display_name': 'Walter Mosley',
'first_name': 'Walter',
'last_name': 'Mosley',
'slug': 'walter-mosley',
'image': 'https://s3.amazonaws.com/8588-book-contributor.jpg',
'social_media_name': '',
'social_media_link': '',
'website': '',
'agents': [{'name': 'Loomis, Gloria',
'primaryemail': 'gloria#gmail.com',
'primaryphone': '212-382-1121'},
{'name': 'Hogson, Barb',
'primaryemail': 'bho124#aol.com',
'primaryphone': ''}],
'estates': [],
'deleted': False}]
Code :
import copy
finalJSON = []
for each in inputJSON:
for agnt in each.get('agents'):
newObj = copy.deepcopy(each)
newObj['agents'] = agnt
finalJSON.append(newObj)
print(finalJSON)

How to access Nested Dictionary values through index

Im building a scanner using python-nmap libary. Here's the code :
import nmap
import json
def Nmap_Recon(host, port):
nm = nmap.PortScanner()
lol = nm.scan(host, '22-443')
print(lol['scan'])
Nmap_Recon('www.stuxnoid.org',80)
Output :
{'77.72.0.90': {'hostnames': [{'name': 'www.stuxnoid.org', 'type': 'user'}, {'name': 'carbon.cloudhosting.co.uk', 'type': 'PTR'}], 'addresses': {'ipv4': '77.72.0.90'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'syn-ack'}, 'tcp': {25: {'state': 'open', 'reason': 'syn-ack', 'name': 'smtp', 'product': '', 'version': '', 'extrainfo': '', 'conf': '3', 'cpe': ''}, 80: {'state': 'open', 'reason': 'syn-ack', 'name': 'http', 'product': 'imunify360-webshield/1.6', 'version': '', 'extrainfo': '', 'conf': '10', 'cpe': ''}, 443: {'state': 'open', 'reason': 'syn-ack', 'name': 'https', 'product': 'imunify360-webshield/1.6', 'version': '', 'extrainfo': '', 'conf': '10', 'cpe': ''}}}}
I guess the output is in dictionary format. The problem is, I want to display only the open port details. But the port details are nested inside the dict_key IP address (77.72.0.90) and It keeps changing with the domain I pass. How to access those Open port details and display them?
if your question is how to get first item out of a dictionary (arbitrary item prior to python 3.6) it can be done so:
next(iter(lol['scan'].values()))
or, destructively (this will return last item):
lol['scan'].popitem()[1]

Categories

Resources