Related
I am trying to pick Instances in the json objects data which looks like this
[{'Groups': [], 'Instances': [{'AmiLaunchIndex': 0, 'ImageId': 'ami-0ceecbb0f30a902a6', 'InstanceId': 'i-xxxxx', 'InstanceType': 't2.micro', 'KeyName': 'xxxx', 'LaunchTime': {'$date': '2022-12-17T13:07:54Z'}, 'Monitoring': {'State': 'disabled'}, 'Placement': {'AvailabilityZone': 'us-west-2b', 'GroupName': '', 'Tenancy': 'default'}, 'PrivateDnsName': 'ip-zxxxxx.us-west-2.compute.internal', 'PrivateIpAddress': 'xxxxx', 'ProductCodes': [], 'PublicDnsName': 'ec2-xx-xxx-xxx.us-west-2.compute.amazonaws.com', 'PublicIpAddress': 'xxxxxx', 'State': {'Code': 16, 'Name': 'running'}, 'StateTransitionReason': '', 'SubnetId': 'subnet-xxxxx', 'VpcId': 'vpc-xxxxx', 'Architecture': 'x86_64', 'BlockDeviceMappings': [{'DeviceName': '/dev/xvda', 'Ebs': {'AttachTime': {'$date': '2022-12-17T13:07:55Z'}, 'DeleteOnTermination': True, 'Status': 'attached', 'VolumeId': 'vol-xxxx'}}], 'ClientToken': '529fc1ac-bf64-4804-b0b8-7c7778ace68c', 'EbsOptimized': False, 'EnaSupport': True, 'Hypervisor': 'xen', 'NetworkInterfaces': [{'Association': {'IpOwnerId': 'amazon', 'PublicDnsName': 'ec2-35-86-111-31.us-west-2.compute.amazonaws.com', 'PublicIp': 'xxxxx'}, 'Attachment': {'AttachTime': {'$date': '2022-12-17T13:07:54Z'}, 'AttachmentId': 'eni-attach-0cac7d4af20664b23', 'DeleteOnTermination': True, 'DeviceIndex': 0, 'Status': 'attached', 'NetworkCardIndex': 0}, 'Description': '', 'Groups': [{'GroupName': 'launch-wizard-5', 'GroupId': 'sg-xxxxx'}], 'Ipv6Addresses': [], 'MacAddress': 'xxxxx', 'NetworkInterfaceId': 'eni-xxxxx', 'OwnerId': 'xxxx', 'PrivateDnsName': 'ip-xxxxx.us-west-2.compute.internal', 'PrivateIpAddress': 'xxx.xxx.xxx', 'PrivateIpAddresses': [{'Association': {'IpOwnerId': 'amazon', 'PublicDnsName': 'ec2-xx-xx-xx-xxx.us-west-2.compute.amazonaws.com', 'PublicIp': 'xxx.xxx.xxx'}, 'Primary': True, 'PrivateDnsName': 'ip-172-31-20-187.us-west-2.compute.internal', 'PrivateIpAddress': 'xxx.xxx.xxx'}], 'SourceDestCheck': True, 'Status': 'in-use', 'SubnetId': 'subnet-xxxxxxx', 'VpcId': 'vpc-0b09cd4sedxxx', 'InterfaceType': 'interface'}], 'RootDeviceName': '/dev/xvda', 'RootDeviceType': 'ebs', 'SecurityGroups': [{'GroupName': 'launch-wizard-5', 'GroupId': 'sg-0a0d1c79d8076660e'}], 'SourceDestCheck': True, 'Tags': [{'Key': 'Name', 'Value': 'MainServers'}], 'VirtualizationType': 'hvm', 'CpuOptions': {'CoreCount': 1, 'ThreadsPerCore': 1}, 'CapacityReservationSpecification': {'CapacityReservationPreference': 'open'}, 'HibernationOptions': {'Configured': False}, 'MetadataOptions': {'State': 'applied', 'HttpTokens': 'optional', 'HttpPutResponseHopLimit': 1, 'HttpEndpoint': 'enabled', 'HttpProtocolIpv6': 'disabled', 'InstanceMetadataTags': 'disabled'}, 'EnclaveOptions': {'Enabled': False}, 'PlatformDetails': 'Linux/UNIX', 'UsageOperation': 'RunInstances', 'UsageOperationUpdateTime': {'$date': '2022-12-17T13:07:54Z'}, 'PrivateDnsNameOptions': {'HostnameType': 'ip-name', 'EnableResourceNameDnsARecord': True, 'EnableResourceNameDnsAAAARecord': False}, 'MaintenanceOptions': {'AutoRecovery': 'default'}}], 'OwnerId': '76979cfxdsss11', 'ReservationId': 'r-xxxxx'}]
I tired loading data and doing
resp = json.loads(jsonfile)
reqData= resp['Instances']
But getting error
TypeError: list indices must be integers or slices, not str
Is there any way I can fix this and get the data? Help will be extremely appriciated.
It's wrapped inside a list. So simply do:
print(lst[0]["Instances"])
To select only the instances from the data, you can use the json.loads function to parse the JSON data and extract the Instances field as a list.
import json
# Parse the JSON data
data = json.loads(json_data)
# Extract the instances
instances = data['Instances']
You can then iterate over the data with something like this
for instance in instances:
instance_id = instance['InstanceId']
instance_type = instance['InstanceType']
launch_time = instance['LaunchTime']
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"])
I'm trying to edit some dictionary values using Regular Expression and change them back into a dict object.
I am checking IP data using Shodan API, this returns a set of results, more than I need.
Shodan Ip
ipinfo {'city': None, 'region_code': None, 'os': None, 'tags': ['vpn'], 'ip': 771247238, 'isp': 'Host Universal Pty', 'area_code': None, 'dma_code': None, 'last_update': '2019-05-01T06:53:53.130508', 'country_code3': 'AUS', 'country_name': 'Australia', 'hostnames': [], 'postal_code': None, 'longitude': 143.2104, 'country_code': 'AU', 'ip_str': '45.248.76.134', 'latitude': -33.494, 'org': 'Host Universal Pty', 'data': [{'_shodan': {'id': 'bc2dc252-5b9d-4b3d-975f-0156860c8849', 'options': {}, 'ptr': True, 'module': 'https', 'crawler': '65e79faecee26516a8ed6f16c1142432f303fbdc'}, 'hash': 0, 'os': None, 'opts': {}, 'ip': 771247238, 'isp': 'Host Universal Pty', 'port': 443, 'hostnames': [], 'location': {'city': None, 'region_code': None, 'area_code': None, 'longitude': 143.2104, 'country_code3': 'AUS', 'country_name': 'Australia', 'postal_code': None, 'dma_code': None, 'country_code': 'AU', 'latitude': -33.494}, 'timestamp': '2019-05-01T06:53:53.130508', 'domains': [], 'org': 'Host Universal Pty', 'data': '', 'asn': 'AS136557', 'transport': 'tcp', 'ip_str': '45.248.76.134'}, {'_shodan': {'id': 'cdce36e7-588f-4377-8cc6-f9bedd426e6b', 'options': {}, 'ptr': True, 'module': 'https', 'crawler': '0636e1e6dd371760aeaf808ed839236e73a9e74d'}, 'hash': 0, 'os': None, 'opts': {}, 'ip': 771247238, 'isp': 'Host Universal Pty', 'port': 8443, 'hostnames': [], 'location': {'city': None, 'region_code': None, 'area_code': None, 'longitude': 143.2104, 'country_code3': 'AUS', 'country_name': 'Australia', 'postal_code': None, 'dma_code': None, 'country_code': 'AU', 'latitude': -33.494}, 'timestamp': '2019-04-26T18:31:18.138759', 'domains': [], 'org': 'Host Universal Pty', 'data': '', 'asn': 'AS136557', 'transport': 'tcp', 'ip_str': '45.248.76.134'}, {'_shodan': {'id': '27e5f5e0-662e-4621-b043-56d64d25f38d', 'options': {}, 'ptr': True, 'module': 'http', 'crawler': 'c9b639b99e5410a46f656e1508a68f1e6e5d6f99'}, 'hash': 0, 'os': None, 'opts': {}, 'ip': 771247238, 'isp': 'Host Universal Pty', 'http': {'robots_hash': None, 'redirects': [], 'securitytxt': None, 'title': None, 'sitemap_hash': None, 'robots': None, 'server': None, 'host': '45.248.76.134', 'html': None, 'location': '/', 'html_hash': None, 'sitemap': None, 'securitytxt_hash': None}, 'port': 8080, 'hostnames': [], 'location': {'city': None, 'region_code': None, 'area_code': None, 'longitude': 143.2104, 'country_code3': 'AUS', 'country_name': 'Australia', 'postal_code': None, 'dma_code': None, 'country_code': 'AU', 'latitude': -33.494}, 'timestamp': '2019-04-21T03:00:14.986062', 'domains': [], 'org': 'Host Universal Pty', 'data': '', 'asn': 'AS136557', 'transport': 'tcp', 'ip_str': '45.248.76.134'}, {'_shodan': {'id': 'bfbc3556-d00d-4512-8cb3-32ef6cae9964', 'options': {}, 'ptr': True, 'module': 'ike', 'crawler': '8cd926590a400feb4b683f8337a77287ddf3d2c7'}, 'hash': -451677272, 'os': None, 'tags': ['vpn'], 'opts': {'raw': '61713862726c6c3764627037343033792920252800000000000000240000000800000005'}, 'ip': 771247238, 'isp': 'Host Universal Pty', 'port': 500, 'isakmp': {'initiator_spi': '61713862726c6c37', 'responder_spi': '6462703734303379', 'msg_id': '00000000', 'next_payload': 41, 'exchange_type': 37, 'length': 36, 'version': '2.0', 'flags': {'encryption': False, 'authentication': False, 'commit': False}, 'aggressive': {'initiator_spi': 'a6517b6a97dca862', 'responder_spi': '1655d8123c9f2104', 'msg_id': 'd14144c6', 'next_payload': 11, 'exchange_type': 5, 'length': 40, 'version': '1.0', 'flags': {'encryption': False, 'authentication': False, 'commit': False}, 'vendor_ids': []}, 'vendor_ids': []}, 'hostnames': [], 'location': {'city': None, 'region_code': None, 'area_code': None, 'longitude': 143.2104, 'country_code3': 'AUS', 'country_name': 'Australia', 'postal_code': None, 'dma_code': None, 'country_code': 'AU', 'latitude': -33.494}, 'timestamp': '2019-04-13T11:18:42.166709', 'domains': [], 'org': 'Host Universal Pty', 'data': 'VPN (IKE)\n\nInitiator SPI: 61713862726c6c37\nResponder SPI: 6462703734303379\nNext Payload: RESERVED\nVersion: 2.0\nExchange Type: DOI Specific Use\nFlags:\n Encryption: False\n Commit: False\n Authentication: False\nMessage ID: 00000000\nLength: 36', 'asn': 'AS136557', 'transport': 'udp', 'ip_str': '45.248.76.134'}], 'asn': 'AS136557', 'ports': [443, 8443, 8080, 500]}
I use Regular Expression to delete the data I don't need. This deletes anything from the fields' data' onwards.
osint_ip1 = re.sub("..'data':.*", "}", str(ipinfo))
Heres the problem... Since Showdan returns inconsistently depending on the IP, I need to use a Dirctwriter to write the corresponding values to there fields.
The problem with this is I have to cast ipinfo as a string to edit the data, and the string object canot be used in the CSV Directwriter.
How do I turn the string back into direction format?
OSINT(STRING)
rejoin : ["{'city': None", " 'region_code': None", " 'os': None", " 'tags': ['vpn']", " 'ip': 771247238", " 'isp': 'Host Universal Pty'", " 'area_code': None", " 'dma_code': None", " 'last_update': '2019-05-01T06:53:53.130508'", " 'country_code3': 'AUS'", " 'country_name': 'Australia'", " 'hostnames': []", " 'postal_code': None", " 'longitude': 143.2104", " 'country_code': 'AU'", " 'ip_str': '45.248.76.134'", " 'latitude': -33.494", " 'org': 'Host Universal Pty'}"]
Full code below
import csv
import os
import re
import time
import shodan
from shodan import Shodan
def OPSINT():
for x in ip:
print(x)
try:
ipinfo = api.host(x)
except shodan.exception.APIError:
ipinfo = None
pass
filename = 'C:\\ProgramData\\FDA\\output\\processed\\OSINT.csv'
if ipinfo != None:
osint_ip1 = re.sub("..'data':.*", "}", str(ipinfo))
osint_ip = osint_ip1.split(',')
print("rejoin :", osint_ip)
# print(osint_ip)
print("ipinfo", ipinfo)
with open("C:\\ProgramData\\FDA\\output\\processed\\OSINT.csv", 'a') as csvfile:
fieldnames = ['city', 'region_code', 'os', 'tags', 'ip', 'isp', 'area_code', 'dma_code', 'last_update',
'country_code3', 'country_name', 'hostnames', 'postal_code', 'longitude', 'country_code',
'ip_str', 'latitude', 'org']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
fileEmpty = os.stat(filename).st_size == 0
if fileEmpty:
writer.writeheader()
else:
writer.writerows(osint_ip)
csvfile.close()
A dict is a well-structured data type meant to be accessed and/or manipulated via keys. Manipulating a dict by converting it to a string and performing regex substitution is wholly unnecessary and error-prone.
Since you've already defined the exact keys you want as fieldnames, you can simply use operator.itemgetter to get the values of these keys from the dict ipinfo, and zip them with the key names to construct a new dict for csv.DictWriter.writerow to write from:
from operator import itemgetter
...
writer.writerow(dict(zip(fieldnames, itemgetter(*fieldnames)(ipinfo))))
So I'm utilizing the GMusicAPI to try to interact with Google Play Music (I'm utilizing the MobileClient view) a bit. Unfortunately it's only in Python, a language I've never used before. I'm running a search, that will search for a given artist. The problem I run into is that, some artists, the artist I want isn't the first result.
My solution around that was to just search (this search returns a dictionary) for the Top 5-10 artists and then loop through them until the name in the dictionary key matches the search query.
Here's the portion of the dictionary I'm getting back from the search. This specifically is the "artist_hits" section.
[{'artist': {'name': 'Tiƫsto', 'artistArtRef': 'http://lh3.googleusercontent.com/wfrs3FuLMoZ7MMfESLOE7kXw9pR9usqZsR-OCo7GW544aqHfj_WMo_YYeETdAmUGQU9fJW7D', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': False, 'url': 'http://lh3.googleusercontent.com/wfrs3FuLMoZ7MMfESLOE7kXw9pR9usqZsR-OCo7GW544aqHfj_WMo_YYeETdAmUGQU9fJW7D', 'aspectRatio': '2'}], 'kind': 'sj#artist', 'artistId': 'Agzf4r7d54azste62qh6z2q7bcy', 'artist_bio_attribution': {'license_url': 'http://creativecommons.org/licenses/by-sa/4.0/legalcode', 'kind': 'sj#attribution', 'source_title': 'Wikipedia', 'license_title': 'Creative Commons Attribution CC-BY-SA 4.0', 'source_url': 'http://en.wikipedia.org/wiki/Ti%C3%ABsto'}}, 'type': '2'},
{'artist': {'name': 'Skrillex', 'artistArtRef': 'http://lh3.googleusercontent.com/wUw1KU9e6VOO7tSNFjKvCljL05Fud1w5mvt8UTbs5G7fBS4sFGphEOIs0EUXBwF9CHuGFEufgQ', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': False, 'url': 'http://lh3.googleusercontent.com/wUw1KU9e6VOO7tSNFjKvCljL05Fud1w5mvt8UTbs5G7fBS4sFGphEOIs0EUXBwF9CHuGFEufgQ', 'aspectRatio': '2'}], 'kind': 'sj#artist', 'artistId': 'Aqy2vtuiohb4rdrakrtbphxbdme', 'artist_bio_attribution': {'kind': 'sj#attribution', 'source_title': 'artist representative'}}, 'type': '2'},
{'artist': {'kind': 'sj#artist', 'artistId': 'Achiqpfv5ncaoobeiu6vfmf2jf4', 'name': 'Marshmello', 'artistArtRef': 'http://lh3.googleusercontent.com/KAqga3a8rX1Tam5FSyWGUcT56Zm9uDoyei2vty1Xra8CApKn1vi5Nb9-nNt70U4Q6rtuOA9KXQ', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/KAqga3a8rX1Tam5FSyWGUcT56Zm9uDoyei2vty1Xra8CApKn1vi5Nb9-nNt70U4Q6rtuOA9KXQ', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/Zp3xcR0sc0LDnFuYy7sY-l8ggvEmPjFK_UruOSaZk25jKIYZKDsZA382WIyXqptClH91oI7QzQ', 'aspectRatio': '1'}]}, 'type': '2'},
{'artist': {'name': 'deadmau5', 'artistArtRef': 'http://lh3.googleusercontent.com/rNh6zCCuiJCgEWldulkYIbpfP33W9K0ULAPsWrDThPdz_naqHR_bznxRFgoOwMRr5V0ur-O-', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': False, 'url': 'http://lh3.googleusercontent.com/rNh6zCCuiJCgEWldulkYIbpfP33W9K0ULAPsWrDThPdz_naqHR_bznxRFgoOwMRr5V0ur-O-', 'aspectRatio': '2'}], 'kind': 'sj#artist', 'artistId': 'Atngmlxlixottlthpcypidmxlu4', 'artist_bio_attribution': {'license_url': 'http://creativecommons.org/licenses/by-sa/4.0/legalcode', 'kind': 'sj#attribution', 'source_title': 'Wikipedia', 'license_title': 'Creative Commons Attribution CC-BY-SA 4.0', 'source_url': 'http://en.wikipedia.org/wiki/Deadmau5'}}, 'type': '2'},
{'artist': {'kind': 'sj#artist', 'artistId': 'A3akm3hkaas6r4mp3nmhn2d7vom', 'name': 'Marshmello', 'artistArtRef': 'http://lh3.googleusercontent.com/sU63V5nC1xAYuYLfd-WCzmfP5Z1iYslm8xntUe6HPVRmvIEs9QCwaxRgcaQy1Eh8HbSEwEMunQ', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/sU63V5nC1xAYuYLfd-WCzmfP5Z1iYslm8xntUe6HPVRmvIEs9QCwaxRgcaQy1Eh8HbSEwEMunQ', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh4.ggpht.com/6cv48SUFmOIXMjXmgs6KCw6NViHEQ2w2YRN2hhQ6HBAcVggh9-L882bYwH5VKZ2YqloodT3rNg', 'aspectRatio': '1'}]}, 'type': '2'},
{'artist': {'kind': 'sj#artist', 'artistId': 'Aatki3zj7b6fnemq2qldpjmfg74', 'name': 'Marshmellow Overcoat', 'artistArtRef': 'http://lh3.googleusercontent.com/-0spMaLVdzkwkoLgfNPAAL-KMUX1ulOYEMN7sAs_i2kZtFhq-CmPCKHECF_wRtKeTUnFWKViPg', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/-0spMaLVdzkwkoLgfNPAAL-KMUX1ulOYEMN7sAs_i2kZtFhq-CmPCKHECF_wRtKeTUnFWKViPg', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.ggpht.com/BfiwEzdKnrAonw4myuyPu6p7GlsAhpd3ZA4E4wQQ-rkygRikCYKRwOWTTM1y3C2oYhPkqXsq0Q', 'aspectRatio': '1'}]}, 'type': '2'},
{'artist': {'kind': 'sj#artist', 'artistId': 'Aik7qbnj6ml3i7fpya4rwxjswqq', 'name': 'Marshmellow', 'artistArtRef': 'http://lh3.googleusercontent.com/9UXduVP_4LP702bqvQKU8NKBh9sH_1P9LLYXYQ_IcRcVQUNXqMiSY1dykYdWlGR21NsakeOK', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/9UXduVP_4LP702bqvQKU8NKBh9sH_1P9LLYXYQ_IcRcVQUNXqMiSY1dykYdWlGR21NsakeOK', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/xmuuJ_XtgD1CBq7ZBKYZdo0gp7s9oiXZeNQ1lEhDfsXIWhAv2JD3w7IyM54y5OFpw8cAOEaV2Q', 'aspectRatio': '1'}]}, 'type': '2'}]
My question is how do I access anything past the first "artist" key? I need to get the "name" and "artistId" values from it. I'm calling the first one like this.
ArtistSearch['artist_hits'][0]['artist']['name']
And that works fine. However I try to access the second instance of that key like this
ArtistSearch['artist_hits'][0]['artist'][1]['name']
And it doesn't work. Any help would be greatly appreciated.
I think what you wish is:
ArtistSearch['artist_hits'][1]['artist']['name']
# ---^---
because ArtistSearch['artist_hits'] is the list, you want the second dictionary [1] and inside it the ['artist']['name'] field
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']}