List Comprehension multiple values/keys - python

I need some advice on the following code. I need to be able to extract two pieces of information but I need them in the same variable, for example,
item_out_list = [{
"fvRsNodeAtt": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "apple",
"value": "mango"
}
}
},
{"fvRsNodeAtt": {
"attributes": {
"annotation": "",
"childAction": "",
"descr": "peach",
"value": "banana"
}
}
}
]
static = [item['fvRsNodeAtt']['attributes']['descr'] for item in item_out_list]
print(static)
So the above gives me the value Apple, which is great but what I want to do is also grab "value" at the same time. I know could run another list and grab the value separate but I need them on same line, so when I print it out, I see apple mango.
I could join them together easily but I thought they might be an easier or efficient way of accessing the extra keys because it might be that I need 3 or 4 keys from the json above as it grows, for example, "annotation", "childAction", "descr", etc.
So the item_out_list is coming from a REST API, so there is multiple entries every time.
So when I lookup the values I want, it might be
apple mango
peach apple
banana orange.
so every time I query the API, I will get a different response, I am then using List comp to pull that out and save to CSV. The CSV isnt the problem, I just need to access more then one value and save to a variable
any help would be great

static = [item_all[item]['attributes']['descr'] + ', ' + item_all[item]['attributes']['value'] for item_all in item_out_list for item in item_all]
print (static)
output:
['apple, mango', 'peach, banana']

here is what you need
static = [list(item_out_list['fvRsNodeAtt']['attributes'].values())[2: 4]]
heres another way
static = [item_out_list['fvRsNodeAtt']['attributes']['descr'] + " " + item_out_list['fvRsNodeAtt']['attributes']['value']]
here how to get everything without looping
static = [item_out_list['fvRsNodeAtt']['attributes']]

Related

Extract data that is inside a bracket

I have been using rapid api to get some data on certain food products and below is an example of the json data i got back. I have been able to get some data such as the ingredients and but where i am struggling is getting the data that are nested inside each other. My question is how would i be able to get for example the data of "amount" which is inside nutrients in python.
"ingredients": "Whole Grain Corn, Sugar, Corn Syrup, Corn Meal"
"nutrition": {
"nutrients": [
{
"name": "Calcium",
"amount": 100.0,
"unit": "mg",
"percentOfDailyNeeds": 10.0
},
{
"name": "Carbohydrates",
"amount": 23.0,
"unit": "g",
"percentOfDailyNeeds": 7.67
},
The way which i was able to get the ingredients was by doing this which worked and printed out the ingredients
ingredients = response.json().get("ingredients")
But how would i do the same thing to get specific data inside nutrients such as the "name" carbohydrates?
It's a list (https://docs.python.org/3/tutorial/datastructures.html).
You can access it by the index (starting at 0). So to get Carbohydrates you would do dictName["nutrition"]["nutrients"][1]["name"]. To get Calcium you would do dictName["nutrition"]["nutrients"][0]["name"].
It's probably easiest to just assign and then loop through with
nutrients = dictName["nutrition"]["nutrients"]
for nutrient in nutrients:
print(nutrient["name"])
You can do this with a filter too, which would look something like this
carbs = list(filter(lambda x: x['name'] == 'Carbohydrates', response.json()['nutrition']['nutrients']))
Which is a bit more compact. The output you get is
[{'name': 'Carbohydrates', 'amount': 23.0, 'unit': 'g', 'percentOfDailyNeeds': 7.67}]
Simplest solution would be to unpack your values one level at a time:
data = response.json()
nutrition = data.get("nutrition", [])
nutrients = [
item
for item in nutrition.get("nutrients", [])
if item.get("name") == "Carbohydrates"
]
if nutrients:
print(nutrients[0])
The trickiest part is working with the array. I've used list comprehension to build a new filtered list (some would prefer filter function), and then printed an item if the list is non-empty

Printing from a dictionary in Python

I am running a method which returns a dictionary which is formed like the following :
{
"intents": [
{
"name": "goodbye",
"created": "2017-08-18T18:09:36.155Z",
"updated": "2017-08-18T18:09:41.755Z",
"description": null
},
{
"name": "hello",
"created": "2017-08-18T18:05:48.153Z",
"updated": "2017-08-18T18:06:06.004Z",
"description": null
}
],
"pagination": {
"refresh_url": "/v1/workspaces/9978a49e-ea89-4493-b33d-82298d3db20d/intents?version=2017-08-21"
}
}
This is all saved in a variable called response, which contains the dictionary for over 200 values.
If I print just "response", it prints all of the above, including "created/updated/description". I just want to print out the name value...and I cannot figure out how to do it.
I have read some posts here and tried the following -
for value in response:
print(value['intent'])
but similarly, this prints out everything (including description/update date etc).
How can I make it only print out the name?
And for a bonus, how can I add the list of names into an array which I can then iterate over?
It appears you want to access the name attribute of each sub-dict in intents. This should work -
for d in response['intents']:
print(d['name'])
If you want this stored in a list, use a list comprehension:
names = [d['name'] for d in response['intents']]
Adding names into list and print it:
names = [intent["name"] for intent in response["intents"]]
print(*names, sep='\n')
Have a look at my solution
names_list = []
for x in response["intents"]:
print x["name"] # it will print all of your names
names_list.append(x["name"]) # it will add the names into the names_list
print names_list
Hope it will help you :)

Python - Searching JSON

I have JSON output as follows:
{
"service": [{
"name": ["Production"],
"id": ["256212"]
}, {
"name": ["Non-Production"],
"id": ["256213"]
}]
}
I wish to find all ID's where the pair contains "Non-Production" as a name.
I was thinking along the lines of running a loop to check, something like this:
data = json.load(urllib2.urlopen(URL))
for key, value in data.iteritems():
if "Non-Production" in key[value]: print key[value]
However, I can't seem to get the name and ID from the "service" tree, it returns:
if "Non-Production" in key[value]: print key[value]
TypeError: string indices must be integers
Assumptions:
The JSON is in a fixed format, this can't be changed
I do not have root access, and unable to install any additional packages
Essentially the goal is to obtain a list of ID's of non production "services" in the most optimal way.
Here you go:
data = {
"service": [
{"name": ["Production"],
"id": ["256212"]
},
{"name": ["Non-Production"],
"id": ["256213"]}
]
}
for item in data["service"]:
if "Non-Production" in item["name"]:
print(item["id"])
Whatever I see JSON I think about functionnal programming ! Anyone else ?!
I think it is a better idea if you use function like concat or flat, filter and reduce, etc.
Egg one liner:
[s.get('id', [0])[0] for s in filter(lambda srv : "Non-Production" not in srv.get('name', []), data.get('service', {}))]
EDIT:
I updated the code, even if data = {}, the result will be [] an empty id list.

how to fetch data from json schema? error shown-TypeError: string indices must be integers

I have a json response from an API in this way:-
{
"meta": {
"code": 200
},
"data": {
"username": "luxury_mpan",
"bio": "Recruitment Agents👑👑👑👑\nThe most powerful manufacturers,\nwe have the best quality.\n📱Wechat:13255996580💜💜\n📱Whatsapp:+8618820784535",
"website": "",
"profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/10895140_395629273936966_528329141_a.jpg",
"full_name": "Mpan",
"counts": {
"media": 17774,
"followed_by": 7982,
"follows": 7264
},
"id": "1552277710"
}
}
I want to fetch the data in "media", "followed_by" and "follows" and store it in three different lists as shown in the below code:--
for r in range(1,5):
var=r,st.cell(row=r,column=3).value
xy=var[1]
ij=str(xy)
myopener=Myopener()
url=myopener.open('https://api.instagram.com/v1/users/'+ij+'/?access_token=641567093.1fb234f.a0ffbe574e844e1c818145097050cf33')
beta=json.load(url)
for item in beta['data']:
list1.append(item['media'])
list2.append(item['followed_by'])
list3.append(item['follows'])
When I run it, it shows the error TypeError: string indices must be integers
How would my loop change in order to fetch the above mentioned values?
Also, Asking out of curiosity:- Is there any way to fetch the Watzapp no from the "BIO" key in data dictionary?
I have referred questions similar to this and still did not get my answer. Please help!
beta['data'] is a dictionary object. When you iterate over it with for item in beta['data'], the values taken by item will be the keys of the dictionary: "username", "bio", etc.
So then when you ask for, e.g., item['media'] it's like asking for "username"['media'], which of course doesn't make any sense.
It isn't quite clear what it is that you want: is it just the stuff inside counts? If so, then instead of for item in beta['data']: you could just say item = beta['data']['counts'], and then item['media'] etc. will be the values you want.
As to your secondary question: I suggest looking into regular expressions.

Python how to handle # in a dictionary

I've got some json from last.fm's api which I've serialised into a dictionary using simplejson. A quick example of the basic structure is below.
{
"artist": "similar": {
"artist": {
"name": "Blah",
"image": [{
"#text": "URLHERE",
"size": "small"
}, {
"#text": "URLHERE",
"size": "medium"
}, {
"#text": "URLHERE",
"size": "large"
}]
}
}
}
Any ideas how I can access the image urls of various different sizes?
Thanks,
Jack
Python does not have any problem with # in strings used as dict keys.
>>> import json
>>> j = '{"#foo": 6}'
>>> print json.loads(j)
{u'#foo': 6}
>>> print json.loads(j)[u'#foo']
6
>>> print json.loads(j)['#foo']
6
There are, however, problems with the JSON you post. For one, it isn't valid (perhaps you're missing a couple commas?). For two, you have a JSON object with the same key "image" three times, which cannot coexist and do anything useful.
In Javascript, these two syntaxes are equivalent:
o.foo
o['foo']
In Python they are not. The first gives you the foo attribute, the second gives you the foo key. (It's debatable whether this was a good idea or not.) In Python, you wouldn't be able to access #text as:
o.#text
because the hash will start a comment, and you'll have a syntax error.
But you want
o['#text']
in any case.
You can get what you want from the image list with a list comprehension. Something like
desired = [x for x in images if minSize < x['size'] < maxSize]
Here, images would be the list of dicts from the inner level of you data structure.

Categories