Related
Im running a python 3.7 script where im trying to remove quotes.
Im trying to add a dynamic list (x and y coordinates) to a json-dictionary but i keep getting quotes surrounding the list. I need the list to be inserted without these quotes.
Here is my code that im running:
#The dynamic list
polygon = ['0.124912491249125', '0.683368336833683', '0.128112811281128', '0.472147214721472', '-0.096909690969097', '0.444344434443444', '-0.196919691969197', '-0.533353335333533', '-0.64996499649965', '-0.427742774277428', '-0.290529052905291', '-0.266726672667267', '-0.237523752375237', '0.64996499649965']
list_polygon = []
for i,k in zip(polygon[0::2], polygon[1::2]):
data_polygon = ('['+str(i), str(k)+']')
data_polygon = str(data_polygon)
list_polygon.append(data_polygon)
list_polygon = str(list_polygon)
list_polygon = list_polygon.replace("'",'').replace("(",'').replace(")",'').replace("[[",'[').replace("]]",']')
cord_data = {'apiVersion': '1.3',
'context': '<client context>',
'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
'configurationStatus': 5,
'profiles': [{'camera': 1,
'filters': [{'active': True,
'data': 1,
'type': 'timeShortLivedLimit'},
{'active': True,
'data': 5,
'type': 'distanceSwayingObject'},
{'active': True,
'data': [5, 5],
'type': 'sizePercentage'}],
'name': 'Profile 1',
'triggers': [{'data': [list_polygon],
'type': 'includeArea'}],
'uid': 1}]},
'method': 'setConfiguration'}
And i get the answer:
{'apiVersion': '1.3', 'context': '<client context>', 'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}], 'configurationStatus': 5, 'profiles': [{'camera': 1, 'filters': [{'active': True, 'data': 1, 'type': 'timeShortLivedLimit'}, {'active': True, 'data': 5, 'type': 'distanceSwayingObject'}, {'active': True, 'data': [5, 5], 'type': 'sizePercentage'}], 'name': 'Profile 1', 'triggers': [{'data': ['[0.124912491249125, 0.683368336833683], [0.128112811281128, 0.472147214721472], [-0.096909690969097, 0.444344434443444], [-0.196919691969197, -0.533353335333533], [-0.64996499649965, -0.427742774277428], [-0.290529052905291, -0.266726672667267], [-0.237523752375237, 0.64996499649965]'], 'type': 'includeArea'}], 'uid': 1}]}, 'method': 'setConfiguration'}
As you see it adds a quote between the brackets (at the start and the end) 'triggers': [{'data': ['[ 0.124912491249125, 0.683368336833683], [0.128112811281128, 0.472147214721472], [-0.096909690969097, 0.444344434443444], [-0.196919691969197, -0.533353335333533], [-0.64996499649965, -0.427742774277428], [-0.290529052905291, -0.266726672667267], [-0.237523752375237, 0.64996499649965 ]']
Instead i want it to look like this:
'triggers': [{'data': [[ 0.124912491249125, 0.683368336833683], [0.128112811281128, 0.472147214721472], [-0.096909690969097, 0.444344434443444], [-0.196919691969197, -0.533353335333533], [-0.64996499649965, -0.427742774277428], [-0.290529052905291, -0.266726672667267], [-0.237523752375237, 0.64996499649965 ]]
The first and second must be paired.
The post i want to send should look like this:
cord_data = {'apiVersion': '1.3',
'context': '<client context>',
'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
'configurationStatus': 5,
'profiles': [{'camera': 1,
'filters': [{'active': True,
'data': 1,
'type': 'timeShortLivedLimit'},
{'active': True,
'data': 5,
'type': 'distanceSwayingObject'},
{'active': True,
'data': [5, 5],
'type': 'sizePercentage'}],
'name': 'Profile 1',
'triggers': [{'data': [[0.124912491249125, 0.683368336833683],
[0.128112811281128, 0.472147214721472],
[-0.096909690969097, 0.444344434443444]
and so on],
'type': 'includeArea'}],
'uid': 1}]},
'method': 'setConfiguration'}
What im i doing wrong?
You make every effort to make it str first, when you need list of lists of floats.
polygon = ['0.124912491249125', '0.683368336833683', '0.128112811281128', '0.472147214721472', '-0.096909690969097', '0.444344434443444', '-0.196919691969197', '-0.533353335333533', '-0.64996499649965', '-0.427742774277428', '-0.290529052905291', '-0.266726672667267', '-0.237523752375237', '0.64996499649965']
list_polygon = [[float(i), float(k)] for i,k in zip(polygon[0::2], polygon[1::2])]
cord_data = {'apiVersion': '1.3',
'context': '<client context>',
'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
'configurationStatus': 5,
'profiles': [{'camera': 1,
'filters': [{'active': True,
'data': 1,
'type': 'timeShortLivedLimit'},
{'active': True,
'data': 5,
'type': 'distanceSwayingObject'},
{'active': True,
'data': [5, 5],
'type': 'sizePercentage'}],
'name': 'Profile 1',
'triggers': [{'data': list_polygon,
'type': 'includeArea'}],
'uid': 1}]},
'method': 'setConfiguration'}
print(cord_data)
And a word of advise - don't forget Floating Point Arithmetic: Issues and Limitations
I guess you'd want something like this:
from pprint import pprint
#The dynamic list
polygon = ['0.124912491249125', '0.683368336833683', '0.128112811281128', '0.472147214721472', '-0.096909690969097', '0.444344434443444', '-0.196919691969197', '-0.533353335333533', '-0.64996499649965', '-0.427742774277428', '-0.290529052905291', '-0.266726672667267', '-0.237523752375237', '0.64996499649965']
list_polygon = []
for i,k in zip(polygon[0::2], polygon[1::2]):
# Don't duplicate convert to `str`, please. Data is already a string.
data_polygon = ([i, k])
# Remove this line, it messes it up if retained
# data_polygon = str(data_polygon)
list_polygon.append(data_polygon)
# Why are we converting all lists to string anyway??
# list_polygon = str(list_polygon)
# list_polygon = list_polygon.replace("'",'').replace("(",'').replace(")",'').replace("[[",'[').replace("]]",']')
# Convert all nested strings to floats
list_polygon = [list(map(float, data_polygon)) for data_polygon in list_polygon]
cord_data = {'apiVersion': '1.3',
'context': '<client context>',
'params': {'cameras': [{'active': True, 'id': 1, 'rotation': 0}],
'configurationStatus': 5,
'profiles': [{'camera': 1,
'filters': [{'active': True,
'data': 1,
'type': 'timeShortLivedLimit'},
{'active': True,
'data': 5,
'type': 'distanceSwayingObject'},
{'active': True,
'data': [5, 5],
'type': 'sizePercentage'}],
'name': 'Profile 1',
# Don't nest it within another list (unless needed)
# 'triggers': [{'data': [list_polygon],
'triggers': [{'data': list_polygon,
'type': 'includeArea'}],
'uid': 1}]},
'method': 'setConfiguration'}
pprint(cord_data)
So I have a complex problem, I have a JSON API response with the key: 'key' in 'persons'
but in some cases there is more than 1 'key', I tried to make a for loop to go through the response but it will print it like 20 times. Also, I have no idea to put those 'keys' in a list or a place where I can access them.
to make it a bit easier i have the response that i get out of my .request
{'id': 'a2b1109e-e142-4559-984c-3f9997b1db6a', 'externalId': None, 'name': 'tyr', 'description': '', 'client': '', 'reference': '56345', 'isMonitoring': False, 'monitoringSince': None, 'hasRiskProfile': True, 'riskProfile': 5, 'monitorFrequency': 4, 'mainBindable': None, 'organizationId': '65647b97-5ada-4362-bb3f-cae016722be6', 'userId': 'dd3cc015-e5cf-4a03-9408-74b102900836', 'createDate': '2021-03-23T11:29:55.2027037Z', 'updateDate': '2021-03-23T11:29:55.2027039Z', 'lastMonitorDate': '2021-03-23T11:29:55.2027039Z', 'persons': [{'firstname': 'ya', 'surname': 'o', 'dateOfBirth': '', 'updatedWithIdinIdentificationRequestPerson': None, 'relatedList': None, 'updatedWithIdinIdentificationRequestPersonDate': None, 'history': [], 'fullname': 'ya o', 'externalId': None, 'key': '8b92c210-eee6-4ab2-a093-48e4428af7f8', 'searchResults': [], 'requests': [], 'createDate': '2021-03-23T11:29:57.3853552Z', 'updateDate': '2021-03-23T11:29:57.3853553Z', 'archivedDate': None, 'isArchived': False, 'hasPepSearchResults': False, 'notes': []}], 'businesses': [], 'monitorIds': [], 'addresses': [], 'notifcations': [], 'monitors': []}
the number of keys can be different each time, so I will need to make a check to see if the key that gets back is already existing if so then do nothing, if the key is not existing then save that key in a variable.
here is a example of a response when there are 2 keys"key",
{'id': '44b2203b-b2c8-41f0-8fde-b697ec02a8c8', 'externalId': None, 'name': 'tyr', 'description': '', 'client': '', 'reference': '56345', 'isMonitoring': False, 'monitoringSince': None, 'hasRiskProfile': True, 'riskProfile': 5, 'monitorFrequency': 4, 'mainBindable': None, 'organizationId': '65647b97-5ada-4362-bb3f-cae016722be6', 'userId': 'dd3cc015-e5cf-4a03-9408-74b102900836', 'createDate': '2021-03-23T20:58:53.0345703Z', 'updateDate': '2021-03-23T20:58:53.0345705Z', 'lastMonitorDate': '2021-03-23T20:58:53.0345706Z', 'persons': [{'firstname': 'marnox', 'surname': 'bolier', 'dateOfBirth': '', 'updatedWithIdinIdentificationRequestPerson': None, 'relatedList': None, 'updatedWithIdinIdentificationRequestPersonDate': None, 'history': [], 'fullname': 'marnox bolier', 'externalId': None, 'key': '68e4a9ad-2431-490f-a216-61a0cbd81c57', 'searchResults': [{'at': '2021-03-23T20:58:54.6796804Z', 'totalHits': 0, 'type': None, 'results': [{'paymentRequired': False, 'service': None, 'source': 'CIR', 'items': [], 'count': 0}, {'paymentRequired': False, 'service': 'VaV61', 'source': 'Entity', 'items': [], 'count': 0}], 'requestable': {'id': 'bf9dea30-2f4a-467a-a7b1-6765ceaee517', 'name': 'marnox bolier', 'createDate': '2021-03-23T20:58:55.5409494Z', 'updateDate': '2021-03-23T20:58:55.5409495Z', 'type': 'NotFoundPerson', 'sourceKey': 'A135D3C449EA15C66B6444611E2C97EC', 'picture': None, 'properties': {}, 'resultKey': None, 'data': None, 'archivedDate': None, 'isArchived': False}, 'cachedResult': False}], 'requests': [{'id': '57866866-85c4-43d8-8caa-b47a90b12be4', 'name': 'marnox bolier (TO UPDATE)', 'createDate': '2021-03-23T20:58:56.3090064Z', 'updateDate': '2021-03-23T20:58:56.3090064Z', 'type': 'NotFoundPerson', 'sourceKey': 'AB1AD3379D84D5D2404CF8326CDA054D', 'picture': None, 'properties': {}, 'resultKey': None, 'data': None, 'archivedDate': None, 'isArchived': False}], 'createDate': '2021-03-23T20:58:53.912784Z', 'updateDate': '2021-03-23T20:58:53.912784Z', 'archivedDate': None, 'isArchived': False, 'hasPepSearchResults': True, 'notes': []}, {'firstname': 'marnix', 'surname': 'bolier', 'dateOfBirth': '', 'updatedWithIdinIdentificationRequestPerson': None, 'relatedList': None, 'updatedWithIdinIdentificationRequestPersonDate': None, 'history': [], 'fullname': 'marnix bolier', 'externalId': None, 'key': 'c0475154-530e-4802-b215-1d26a2c7f208', 'searchResults': [], 'requests': [], 'createDate': '2021-03-23T20:58:56.9078002Z', 'updateDate': '2021-03-23T20:58:56.9078002Z', 'archivedDate': None, 'isArchived': False, 'hasPepSearchResults': False, 'notes': []}], 'businesses': [], 'monitorIds': [], 'addresses': [], 'notifcations': [], 'monitors': []}
update i solved my first problem getting more than 1 key with a for loop:
for persons in api_response['persons']:
print(persons['key'])
output:
0b8eb227-0105-40a3-bc8b-8e3ef345a3f3
d9b68e7a-ffdd-44ea-86a4-ea4c541146b4
how can I only save the last result so in this case
d9b68e7a-ffdd-44ea-86a4-ea4c541146b4
Assuming you are using requests to fetch the data from the api your response will simply have a json() function attached. Meaning you can do something like this:
import requests
response = requests.get("example.com/api/random/endpoint")
data = response.json()
The json function in turn does the equivalent of json.loads() with the content of the response. Assuming your API returns a JSON object this will be converted to a dictionary (in accordance with the json conversion table.
After that you can follow go2nirvana's answer and manipulate this dictionary as you would any dict. For example using data.pop("key") to remove the value at that key, iterate over the dictionary, or whatever else you can do with a dictionary.
Admittedly your question is a bit vague and I am not sure exactly what you want to do with the data you recieve, but this is how you get that data in a form that is easy to work with within python.
I am new to JSON in Python (forgive me if I word something incorrectly) and I am trying to parse JSON information I get from an API. I get the API information successfully, but when I attempt to extract the information that I need (specifically the 'name' value in the variable info). I can't seem to find anything on how to access it. The code is below:
# Importing JSON handling library for parsing GET request using requests library
import json
import requests
import ast
import collections
# API Endpoint
url = "https://fnbr.co/api/shop"
# API key
key = "" # key is usually here
# Defining Parameters
headers = {"x-api-key":key}
# Sending GET request
req = requests.get(url = url, headers = headers)
info = req.json()
info = info['data']['featured']
print(info)
And the output:
[{'id': '5e8743597576ec53deb18eeb', 'name': 'Deadpool Gear Bundle', 'price': '2,000', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/bundle/5e8743597576ec53deb18eeb/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'bundle', 'slug': 'deadpool-gear-bundle', 'readableType': 'Bundle', 'description': False, 'bundleSet': '5e87444f7576ec029cb18eef', 'bannerText': False, 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e8fbb5d3cdac627afdebeb5', 'name': 'Deadpool Mashups', 'price': '2,000', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/bundle/5e8fbb5d3cdac627afdebeb5/icon.png', 'png': False, 'gallery': False, 'featured': False}, 'rarity': 'marvel', 'type': 'bundle', 'slug': 'deadpool-mashups', 'readableType': 'Bundle', 'description': 'Outfit Bundle.', 'bundleSet': '5e8fba7a3cdac6863bdebeaf', 'bannerText': False, 'history': {'occurrences': 1, 'firstSeen': '2020-04-10T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z']}}, {'id': '5e8742117576ec88f6b18ee0', 'name': 'Chimichanga!', 'price': '300', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/emote/5e8742117576ec88f6b18ee0/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'emote', 'slug': 'chimichanga', 'readableType': 'Emote', 'description': 'Too hot! Too hot!', 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e8741f37576ecfc79b18edd', 'name': "Scootin'", 'price': '500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/emote/5e8741f37576ecfc79b18edd/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'emote', 'slug': 'scootin', 'readableType': 'Emote', 'description': 'Dangerously dainty.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e87422b7576ec21f5b18ee3', 'name': 'Dragacorn', 'price': '1,500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/glider/5e87422b7576ec21f5b18ee3/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'glider', 'slug': 'dragacorn', 'readableType': 'Glider', 'description': 'Bask in its mutant rainbow glory.', 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5e8fbafd3cdac6ed02debeb3', 'name': 'Cuddlepool', 'price': '1,500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/outfit/5e8fbafd3cdac6ed02debeb3/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/outfit/5e8fbafd3cdac6ed02debeb3/featured.png', 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'outfit', 'slug': 'cuddlepool', 'readableType': 'Outfit', 'description': 'Ferociously furry.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 1, 'firstSeen': '2020-04-10T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z']}}, {'id': '5e8fb9f33cdac69b0fdebead', 'name': 'Ravenpool', 'price': '1,500', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/outfit/5e8fb9f33cdac69b0fdebead/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/outfit/5e8fb9f33cdac69b0fdebead/featured.png', 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'outfit', 'slug': 'ravenpool', 'readableType': 'Outfit', 'description': 'Maximum darkness.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 1, 'firstSeen': '2020-04-10T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z']}}, {'id': '5e8741d67576ecad85b18eda', 'name': 'Meaty Mallets', 'price': '800', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/pickaxe/5e8741d67576ecad85b18eda/icon.png', 'png': False, 'gallery': False, 'featured': False, 'resizeAvailable': True}, 'rarity': 'marvel', 'type': 'pickaxe', 'slug': 'meaty-mallets', 'readableType': 'Pickaxe', 'description': "Give 'em a thwack.", 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 4, 'firstSeen': '2020-04-03T00:00:00.471Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2020-04-05T00:00:00.000Z', '2020-04-04T00:00:00.000Z', '2020-04-03T00:00:00.471Z']}}, {'id': '5db7ff1ac02d0515d20b054c', 'name': 'Riley', 'price': '1,200', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/outfit/5db7ff1ac02d0515d20b054c/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/outfit/5db7ff1ac02d0515d20b054c/featured.png', 'resizeAvailable': True}, 'rarity': 'rare', 'type': 'outfit', 'slug': 'riley', 'readableType': 'Outfit', 'description': 'The city is her playground.', 'bundleSet': False, 'bannerText': False, 'history': {'occurrences': 5, 'firstSeen': '2019-11-27T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2019-11-27T00:00:00.000Z', '2020-03-09T00:00:00.000Z', '2020-01-01T00:00:00.000Z', '2020-02-06T00:00:00.575Z']}}, {'id': '5db804ebc02d05322e0b059d', 'name': 'Wild X', 'price': '300', 'priceIcon': 'vbucks', 'priceIconLink': 'https://image.fnbr.co/price/icon_vbucks.png', 'images': {'icon': 'https://image.fnbr.co/wrap/5db804ebc02d05322e0b059d/icon.png', 'png': False, 'gallery': False, 'featured': 'https://image.fnbr.co/wrap/5db804ebc02d05322e0b059d/featured.png'}, 'rarity': 'uncommon', 'type': 'wrap', 'slug': 'wild-x', 'readableType': 'Wrap', 'description': 'Show your style.', 'bundleSet': False, 'bannerText': 'Collect the Set!', 'history': {'occurrences': 5, 'firstSeen': '2019-11-27T00:00:00.000Z', 'lastSeen': '2020-04-10T00:00:00.000Z', 'dates': ['2020-04-10T00:00:00.000Z', '2019-11-27T00:00:00.000Z', '2020-03-09T00:00:00.000Z', '2020-01-01T00:00:00.000Z', '2020-02-06T00:00:00.575Z']}}]
I need specifically all of the 'name' value attributes, such as 'Deadpool Gear Bundle', but I'm unsure how. If I try accessing the location info['data']['featured']['name'] location it gives me an error. Also, there could be a different amount of 'name' values every day, so I need a method that returns the attributes of all of them.
So, there's 2 types of objects you need to be aware of.
dict: when you load JSON it's stored as a dictionary object. Dict objects let you access the values through keys, like you're doing here -> info['data']['featured']
list: print(info) is showing that 'info' is a list of things. You can tell by the square brackets [], or just by calling print(type(info)). A list is ordered so to access the name for the first object in your list you would say info[0]['name'].
To get all the objects in your list in a row you can use a for loop:
for x in info:
print(x['name'])
You can name 'x' whatever you want. That loop is just saying " for every object in this list, perform the following action"
I'm a beginner at python, and I'm working on a project that parses comments from an Instagram post to determine who commented first. I have everything working except for the parsing of the large text file I get when I pull the comments from the Instagram API. I tried using a split statement to split by the instance of "id", but that didn't do what I want, and I can't seem to figure out how to do this.
Text file I'm trying to parse is here: https://pastebin.com/SjyaUZ1u
Code is below
with open("comments.txt", "r") as f:
for line in f:
str = line.split("id", 10)
print(str)
Eventually I'd like to output these instances along with following text into another file, but this is the main problem right now.
using BeautifulSoup and its html.parser parse your html
find the json content inside pre tag
convert it into dictionary format
get the data using keys.
from bs4 import BeautifulSoup
import json
content = open('comments.txt')
soup = BeautifulSoup(content, 'html.parser')
a = soup.find('pre').get_text()
# print(a)
data = json.loads(a)
# print(data['graphql']['shortcode_media']['edge_media_to_comment']['edges'])
comments = data['graphql']['shortcode_media']['edge_media_to_comment']['edges']
for i in comments:
print(i)
output:
{'node': {'id': '17866050361316207', 'text': '#pay2on gimme that coin', 'created_at': 1549838809, 'did_report_as_spam': False, 'owner': {'id': '351807810', 'is_verified': False, 'profile_pic_url': 'https://scontent-dfw5-1.cdninstagram.com/vp/3d9c34d98843652774cbe4ed24111890/5CDB0369/t51.2885-19/s150x150/46791596_1837989999663259_8814990427338833920_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com', 'username': 'pay2on'}, 'viewer_has_liked': False, 'edge_liked_by': {'count': 0}}}
{'node': {'id': '18015390769112027', 'text': 'Gg', 'created_at': 1549838810, 'did_report_as_spam': False, 'owner': {'id': '1620477479', 'is_verified': False, 'profile_pic_url': 'https://scontent-dfw5-1.cdninstagram.com/vp/386991ecd3581167df6fbcdcc3fbffba/5CDA79E7/t51.2885-19/s150x150/43093738_486110321900668_5555852948304560128_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com', 'username': 'a_andrestares'}, 'viewer_has_liked': False, 'edge_liked_by': {'count': 0}}}
{'node': {'id': '18032678767050139', 'text': '💀', 'created_at': 1549838821, 'did_report_as_spam': False, 'owner': {'id': '10336797539', 'is_verified': False, 'profile_pic_url': 'https://scontent-dfw5-1.cdninstagram.com/vp/a76a0efe6e172d370cd1e78d91fd0dd8/5CF3F51A/t51.2885-19/50496250_242828456650749_2095120626497880064_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com', 'username': 'sourpods'}, 'viewer_has_liked': False, 'edge_liked_by': {'count': 0}}}
{'node': {'id': '18031342408039828', 'text': '#pay2on anticoin', 'created_at': 1549838844, 'did_report_as_spam': False, 'owner': {'id': '4726002591', 'is_verified': False, 'profile_pic_url': 'https://scontent-dfw5-1.cdninstagram.com/vp/124d070772b728492cb4a2aa00ff7919/5D01194C/t51.2885-19/s150x150/49303395_379094076216927_6321318684270788608_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com', 'username': 'anti.tv'}, 'viewer_has_liked': False, 'edge_liked_by': {'count': 0}}}
{'node': {'id': '17900497066291896', 'text': 'H9', 'created_at': 1549838912, 'did_report_as_spam': False, 'owner': {'id': '9667789306', 'is_verified': False, 'profile_pic_url': 'https://scontent-dfw5-1.cdninstagram.com/vp/de6dfba0978218656579b080ee53f8c4/5CE83473/t51.2885-19/s150x150/50746304_2379848008910407_7742041896683307008_n.jpg?_nc_ht=scontent-dfw5-1.cdninstagram.com', 'username': 'chronic_wanna_be'}, 'viewer_has_liked': False, 'edge_liked_by': {'count': 0}}}
End goal: I want to get 'userid', 'username', and 'email' from a JSON string that I pulled from Slack. When I try converting it to a list, it only gives me the first three keys, it won't pull the nested data.
Here's my code:
from slackclient._client import SlackClient
import json
def main():
token = "XXXXX"
sc = SlackClient(token)
users = get_users(sc)
print(users)
print(list(users))
def get_users(sc):
print("Get Users")
print(80 * "=")
#call the users.list api call and get list of users
users = (sc.api_call("users.list"))
users = json.dumps(users)
users = json.loads(str(users))
return users
main()
My problem is "print(users)" prints me out an entire JSON string and "print(list(users))" only prints the first un-nested keys. Here's an example of what my output is.
"print(list(users))" gives me the following:
['members', 'cache_ts', 'ok']
"print(users)" gives me the following, but I can't pull anything specific from it as a string.
{'members': [{'real_name': 'XXXXXX', 'is_ultra_restricted': False, 'name': 'XXXXX', 'is_primary_owner': False, 'id': 'XXXXXX', 'color': '4bbe2e', 'status': None, 'profile': {'image_32': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0006-32.png', 'real_name': 'XXXXXX', 'first_name': 'XXXXXX', 'avatar_hash': 'g140a3a97c34', 'email': 'XXXXXXX#XXXXX.COM', 'image_192': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0006-192.png', 'image_24': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0006-24.png', 'real_name_normalized': 'XXXXXX', 'image_512': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0006-512.png', 'last_name': 'XXXXXX', 'image_48': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0006-48.png', 'image_72': 'https://secure.gravatar.com/avatar/140a3a97c34e956bb6692fb0f1bd325e.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0006-72.png'}, 'deleted': False, 'tz': 'America/Los_Angeles', 'tz_offset': -28800, 'is_restricted': False, 'is_admin': False, 'team_id': 'T3854BB3K', 'has_2fa': False, 'is_bot': False, 'tz_label': 'Pacific Standard Time', 'is_owner': False}, {'real_name': 'XXXXXXX, 'is_ultra_restricted': False, 'name': 'XXXXXXX', 'is_primary_owner': True, 'id': 'XXXXXXX', 'color': '9f69e7', 'status': None, 'profile': {'image_32': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0001-32.png', 'real_name': 'XXXXXX', 'first_name': 'XXXXX', 'avatar_hash': 'g98e886a882b', 'email': 'XXXXXXX#gmail.com', 'image_192': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0001-192.png', 'image_24': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2F66f9%2Fimg%2Favatars%2Fava_0001-24.png', 'real_name_normalized': 'XXXXXXX', 'image_512': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F7fa9%2Fimg%2Favatars%2Fava_0001-512.png', 'last_name': 'XXXXXX', 'image_48': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F0180%2Fimg%2Favatars%2Fava_0001-48.png', 'image_72': 'https://secure.gravatar.com/avatar/98e886a882b9eb8476b39519a0007b50.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F3654%2Fimg%2Favatars%2Fava_0001-72.png'}, 'deleted': False, 'tz': 'America/Chicago', 'tz_offset': -21600, 'is_restricted': False, 'is_admin': True, 'team_id': 'T3854BB3K', 'has_2fa': False, 'is_bot': False, 'tz_label': 'Central Standard Time', 'is_owner': True}, {'real_name': 'Talla', 'is_ultra_restricted': False, 'name': 'talla', 'is_primary_owner': False, 'id': 'XXXXXX', 'color': 'e7392d', 'status': None, 'profile': {'api_app_id': 'A0P3RDP0R', 'image_32': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_32.png', 'image_original': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_original.png', 'real_name': 'Talla', 'always_active': False, 'first_name': 'Talla', 'bot_id': 'B38SQ12EN', 'avatar_hash': '0864de31dc83', 'image_1024': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_512.png', 'image_192': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_192.png', 'image_24': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_24.png', 'real_name_normalized': 'Talla', 'image_512': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_512.png', 'image_48': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_48.png', 'image_72': 'https://avatars.slack-edge.com/2016-11-30/111583483766_0864de31dc83b2d4ad5b_72.png'}, 'deleted': False, 'tz': None, 'tz_offset': -28800, 'is_restricted': False, 'is_admin': False, 'team_id': 'T3854BB3K', 'is_bot': True, 'tz_label': 'Pacific Standard Time', 'is_owner': False}, {'real_name': 'slackbot', 'is_ultra_restricted': False, 'name': 'slackbot', 'is_primary_owner': False, 'id': 'USLACKBOT', 'color': '757575', 'status': None, 'profile': {'image_32': 'https://a.slack-edge.com/2fac/plugins/slackbot/assets/service_32.png', 'real_name': 'slackbot', 'first_name': 'slackbot', 'fields': None, 'last_name': '', 'image_192': 'https://a.slack-edge.com/66f9/img/slackbot_192.png', 'image_24': 'https://a.slack-edge.com/0180/img/slackbot_24.png', 'real_name_normalized': 'slackbot', 'image_512': 'https://a.slack-edge.com/1801/img/slackbot_512.png', 'avatar_hash': 'sv1444671949', 'image_48': 'https://a.slack-edge.com/2fac/plugins/slackbot/assets/service_48.png', 'image_72': 'https://a.slack-edge.com/0180/img/slackbot_72.png'}, 'deleted': False, 'tz': None, 'tz_offset': -28800, 'is_restricted': False, 'is_admin': False, 'team_id': 'T3854BB3K', 'is_bot': False, 'tz_label': 'Pacific Standard Time', 'is_owner': False}], 'ok': True, 'cache_ts': 1480600947}
json.loads() returns a dictionary, and by default iterating a dictionary is actually iterating over the keys.
try iterating over users.items() to get (key,value) pairs.