I'm trying to push data to Firebase and I was able to loop through an array and push the information on each loop.
But I need to add some pictures in this (So it'd be like looping an array inside a dictionary definition). I have all the links in an array.
This is my code so far.
def image_upload():
for i in range(len(excel.name)):
doc_ref = db.collection('plans').document('doc-name').collection('collection-name').document()
doc_id = doc_ref.id
data = {
'bedroomLocation': excel.bedroomLocation[i],
'bedrooms': excel.bedrooms[i],
'brand': excel.brand[i],
'catalog': excel.catalog[i],
'category': excel.category[i],
'code': excel.code[i],
'depth': excel.depth[i],
'description': excel.description[i],
'fullBaths': excel.fullBaths[i],
'garage': excel.garage[i],
'garageBays': excel.garageBays[i],
'garageLocation': excel.garageLocation[i],
'garageType': excel.garageType[i],
'date': firestore.SERVER_TIMESTAMP,
'id': doc_id,
'halfBaths': excel.halfBaths[i],
'laundryLocation': excel.laundryLocation[i],
'name': excel.name[i],
'onCreated': excel.onCreated[i],
'productType': excel.productType[i],
'region': excel.region[i],
'sqf': excel.sqf[i],
'state': excel.state[i],
'stories': excel.stories[i],
'tags': [excel.tags[i]],
'width': excel.width[i],
}
doc_ref.set(data)
That works fine, but I don't really know how to loop through the array of links.
This is what I tried below the block I copied above.
for j in range(len(excel.gallery)):
if len(excel.gallery[j]) != 0:
for k in range(len(excel.gallery[j])):
data['gallery'] = firestore.ArrayUnion(
[{'name': excel.gallery[j][k][0], 'type': excel.gallery[j][k][1],
'url': excel.gallery[j][k][2]}])
print(data)
doc_ref.set(data)
len(excel.gallery) has the same length as len(excel.name)
each j position has different amount of links though.
If I declare the gallery inside the data definition and I use ArrayUnion and pre define more than one piece of information it works fine, but I need to use that array to push information to Firebase.
excel.gallery is a matrix actually, is not a dictionary. And this is one of the example outputs for this [[['Images/CFK_0004-Craftmark_Oakmont_Elevation_1.jpeg', 'Elevation', 'url example from firebase'], .... and it goes on for each file. I'm testing with 8 images and 2 plans. So my matrix is 2x4 in this case. But it can happen that in a position there won't be any files if none match. What I'm looking for is add to the data before it is pushed (or after it doesn't matter the order) all the urls for that plan.
This works:
'gallery': firestore.ArrayUnion(
[{'name': 'Example Name', 'type': 'Elevation',
'url': 'Example url'},
{'name': 'Example Name2', 'type': 'First Floor',
'url': 'Example url2'}])
But I need to populate that field looping through excel.gallery
I'm a little confused by when you say, "I have all the links in an array". Could we see that array and what kind of output you are looking for?
Also assuming that excel.gallery is a dictionary you could clean up your code substantially using the items() function.
for j, k in excel.gallery.items():
if k:
data['gallery'] = firestore.ArrayUnion([{'name': k[0], 'type': k[1], 'url': k[2]}])
print(data)
doc_ref.set(data)
Related
I am currently attempting to extract information from the following data set formatted as a list where each news article is its own dictionary:
news_data = [{'source': {'id': 'the-verge', 'name': 'The Verge'}, 'author': 'Emma Roth', 'title': "Judge rules Tesla can't hide behind arbitration in sexual harassment case - The Verge", 'description': 'A lawsuit accusing Tesla of fostering a work environment with “rampant” sexual harassment will continue in court after a judge blocked Tesla’s request for arbitration.', 'url': 'https://www.theverge.com/2022/5/24/23140051/judge-rules-tesla-hide-behind-arbitration-sexual-harassment-case-elon-musk', 'urlToImage': 'https://cdn.vox-cdn.com/thumbor/t3DT8qyznxCW4ahGTwGCSC4l56s=/0x146:2040x1214/fit-in/1200x630/cdn.vox-cdn.com/uploads/chorus_asset/file/10752835/acastro_180430_1777_tesla_0001.jpg', 'publishedAt': '2022-05-24T22:16:03Z', 'content': 'Tesla cant dismiss this case so easily\r\nIllustration by Alex Castro / The Verge\r\nA lawsuit that accuses Tesla of fostering a workplace with rampant sexual harassment will continue in court after a Ca… [+2274 chars]'}, {'source': {'id': 'bloomberg', 'name': 'Bloomberg'}, 'author': None, 'title': 'Elon Musk Drops Out of $200 Billion Club Again as Tesla Tumbles - Bloomberg', 'description': None, 'url': 'https://www.bloomberg.com/tosv2.html?vid=&uuid=38abbf6a-dbe7-11ec-9ad9-767145594c47&url=L25ld3MvYXJ0aWNsZXMvMjAyMi0wNS0yNC9lbG9uLW11c2stZHJvcHMtb3V0LW9mLTIwMC1iaWxsaW9uLWNsdWItYWdhaW4tYXMtdGVzbGEtdHVtYmxlcw==', 'urlToImage': None, 'publishedAt': '2022-05-24T21:11:29Z', 'content': "To continue, please click the box below to let us know you're not a robot."}]
Specifically, I want to extract only the keys 'title' and 'description', saving them into a list for use later.
I've attempted to do this with the following list comprehension:
news_info = [((k,v) for (k,v) in article if k in ['title', 'description']) for article in news_data]
However, if I print the result, I am simply informed:
[<generator object <listcomp>.<genexpr> at 0x102c27840>, <generator object <listcomp>.<genexpr> at 0x102c26dc0>]
Furthermore, if I attempt to access information (e.g. print(news_info[0]['title'])) there is a "TypeError: 'generator' object is not subscriptable".
I was wondering how I can go about printing and accessing/using the information that is saved in the list.
I'm not 100% on the intend, but from what you've tried it seems like you are looking for the result that this should present you:
news_info = [{'title': article['title'], 'description': article['description']} for article in news_data]
print(news_info[0]['title'])
I'd recommend using a function to create the dictionary though.
((k,v) for (k,v) in article if k in ['title', 'description']) - this piece of code creates generator expression. That's why you are getting generator objects.
Another mistake is that you are iterating over dictionary keys, not k,v pairs.
Taking both into account that is what it should look like:
news_info = [(k,v) for article in news_data for (k,v) in article.items() if k in ['title', 'description']]
Your outer () inside the comprehension is creating a generator. If you're expecting a tuple to be created inside here, then just append the word tuple to the beginning.
news_info = [tuple((k,v) for (k,v) in article.items() ...
You also had an issue in that you need to be iterating over the article items.
I am hoping someone can help me solve this problem I am having with a nested JSON response. I have been trying to crack this for a few weeks now with no success.
Using a sites API I am trying to create a dictionary which can hold three pieces of information, for each user, extracted from the JSON responses. The first JSON response holds the users uid and crmid that I require.
The API comes back with a large JSON response, with an object for each account. An extract of this for a single account can be seen below:
{
'uid': 10,
'key':
'[
N#839374',
'customerUid': 11,
'selfPaid': True,
'billCycleAllocationMethodUid': 1,
'stmtPaidForAccount': False,
'accountInvoiceDeliveryMethodUid': 1,
'payerAccountUid': 0,
'countryCode': None,
'currencyCode': 'GBP',
'languageCode': 'en',
'customFields':
{
'field':
[{
'name': 'CRMID',
'value': '11001'
}
]
},
'consentDetails': [],
'href': '/accounts/10'}
I have made a loop which extracts each UID for each account:
get_accounts = requests.get('https://website.com/api/v1/accounts?access_token=' + auth_key)
all_account_info = get_accounts.json()
account_info = all_account_info['resource']
account_information = {}
for account in account_info:
account_uid = account['uid']
I am now trying to extract the CRMID value, in this case '11001': {'name': 'CRMID', 'value': '11001'}.
I have been struggling all week to make this work, I have two problems:
I would like to extract the UID (which I have done) and the CRMID from the deeply nested 'customFields' dictionary in the JSON response. I manage to get as far as ['key'][0], but I am not sure how to access the next dictionary that is nested in the list.
I would like to store this information in a dictionary in the format below:
{'accounts': [{'uid': 10, 'crmid': 11001, 'amount': ['bill': 4027]}{'uid': 11, 'crmid': 11002, 'amount': ['bill': 1054]}]}
(The 'bill' information is going to come from a separate JSON response.)
My problem is, with every loop I design the dictionary seems to only hold one account/the last account it loops over. I cant figure out a way to append to the dictionary instead of overwrite whilst using a loop. If anyone has a useful link on how to do this it would be much appreciated.
My end goal is to have a single dictionary which holds the three pieces of information for each account (uid, crmid, bill). I'm then going to export this into a CSV document.
Any help, guidance, useful links etc would be much appreciated.
In regards to question 1, it may be helpful to print each level as you go down, then try and work out how to access the object you are returned at that level. If it is an array it will using number notation like [0] and if it is a dictionary it will use key notation like ['key']
Regarding question 2, your dictionary needs unique keys. You are probably looping over and replacing the whole thing each time.
The final structure you suggest is a bit off, imagine it as:
accounts: {
'10': {
'uid': '10',
'crmid': 11001,
'amount': {
'bill': 4027
}
},
'11': {
'uid': '11',
'crmid': 11011,
'amount': {
'bill': 4028
}
}
}
etc.
So you can access accounts['10']['crmid'] or accounts['10']['amount']['bill'] for example.
I'm looking at Spotify music with python. Haven't been able to find an answer online so far. I'd like to get information from each song retrieved from the API (2000 songs). I've got the artist of every song, which is a long string of json seen below:
print(artist):
#This is the output:
[[{'track': {'album': {'artists': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/1uiEZYehlNivdK3iQyAbye'},
'href': 'https://api.spotify.com/v1/artists/1uiEZYehlNivdK3iQyAbye',
'id': '1uiEZYehlNivdK3iQyAbye',
'name': 'Tom Misch',
'type': 'artist',
'uri': 'spotify:artist:1uiEZYehlNivdK3iQyAbye'},
{'external_urls': {'spotify': 'https://open.spotify.com/artist/2rspptKP0lPBdlJJAJHqht'},
'href': 'https://api.spotify.com/v1/artists/2rspptKP0lPBdlJJAJHqht',
'id': '2rspptKP0lPBdlJJAJHqht',
'name': 'Yussef Dayes',
'type': 'artist',
'uri': 'spotify:artist:2rspptKP0lPBdlJJAJHqht'}]}}},
# and so on
Since the artist variable is a list of lists & dictionaries, when I print the length of artist I get 20:
len(artist)
20
However, within each element of the artist variable, there are 100 items:
len(artist[0])
100
I want to loop through all 2000 items in artist, and add them to one list. So far my code has been very clunky:
artist_list = []
i=0
while i<100:
artist_list.append(artist[0][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[1][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[2][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[3][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[4][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[5][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[6][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[7][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[8][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[9][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[10][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[11][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[12][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[13][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[14][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[15][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[16][i]["track"]["album"]["artists"][0]["name"])
artist_list.append(artist[17][i]["track"]["album"]["artists"][0]["name"])
i+=1
What's the best way to shorten this? I've tried using enumerate but couldn't quite figure how to do it. Any help would be much appreciated!
You can achieve this using a double for loop:
artist_list = []
for a in artist:
for b in a:
artist_list.append(b["track"]["album"]["artists"][0]["name"])
However, it's more pythonic (and efficient) to use a python list comprehension:
artist_list = [b["track"]["album"]["artists"][0]["name"] for a in artist for b in a]
I get this this error and I cant seem to see what direction I need to go to solve the issue
TypeError: 'DictWrapper' object is not subscriptable
I get the error when trying to extract some data from the results of a lookup from an amazon seller api using this python code:
products_api = mws.Products(access_key, secret_key, seller_id, region='US')
products = products_api.list_matching_products(marketplaceid=marketplace_usa, query='XAZ')
pp = pprint.PrettyPrinter(indent=1)
pp.pprint (vars(products)) # prints info below which contains the data I need
I figured I could use something like the command below to start to make my way through the results but i get the above error and google didnt put me a direction where I could solve the problem. Ultimately I want to extract the 'brand' and a few other fields from the results.
a=products['_mydict']
..
{'_mydict': {'ListMatchingProductsResponse': {'ListMatchingProductsResult': {'Products': {'Product': [{'AttributeSets': {'ItemAttributes': {'Binding': {'value': 'Apparel'},
'Brand': {'value': 'Crocs'},
'Color': {'value': 'Tropical '
'Teal'},
'Department': {'value': 'unisex-adult'},
'IsAdultProduct': {'value': 'false'},
'ItemDimensions': {'Height': {'Units': {'value': 'inches'},
'value': '0.39'},
'Length': {'Units': {'value': 'inches'},
'value': '0.39'},
'Weight': {'Units': {'value': 'pounds'},
'value': '0.44'},
'Width': {'Units': {'value': 'inches'},
'value': '0.39'}},
'Label': {'value': 'crocs'},
'ListPrice': {'Amount': {'value': '34.99'},.......
Looking at the implementation of DictWrapper in the python-amazon-mws library, the object received as a response is not a dictionary and shouldn't be used as one. This is also what the error message states.
However, the object has a property parsed which returns the response in the format of a dictionary. That is what you should use.
products = products_api.list_matching_products(marketplaceid=marketplace_usa, query='XAZ')
products_as_dict = products.parsed
products_as_dict['_mydict']
The object also contains an attribute offering the original XML output, in case you ever need it.
products_as_xml = products.original
The dict is really complex. I reduced it a little, but this should work for you aswell:
print(products['_mydict']['ListMatchingProductsResponse']['ListMatchingProductsResult']['Products']['Product'][0]['AttributeSets']['ItemAttributes']['Brand'])
I think the problem is the nested list. In my example I simply used the first item [0]. Maybe you will need an iteration here.
It prints:
{'value': 'Crocs'}
I have a image template as shown in the picture. I want to get the disk space and the virtual disks list as marked in the Figure. I used:
http://sldn.softlayer.com/zh/reference/services/SoftLayer_Virtual_Guest_Block_Device_Template_Group/getObject to get the image template with mask as ('mask[id,accountId,name,globalIdentifier,blockDevices[device,diskImageId,diskSpace,groupId,id,units],parentId,createDate,datacenter,imageType,storageRepository,datacenters]').
The mask blockDevices is set. But the result is as flows:
{'accountId': xxxxxxx,
'blockDevices': [],
'createDate': '2016-09-18T07:16:57-05:00',
'datacenters': [{'id': xx4092,
'longName': 'Singapore 1',
'name': 'sng01',
'statusId': 2}],
'globalIdentifier': 'xxxxxxxx-b068-40b1-8377-9ab66df80131',
'id': 1331697,
'imageType': {'description': 'a disk that may be replaced on upgrade',
'keyName': 'SYSTEM',
'name': 'System'},
'name': 'xxx-test-all-disk',
'parentId': ''}
The item of blockDevices is a empty array. Why?
Any api can help me to get the image disk space and virtual disks info ?
Please try the following mask (This a Rest example):
https://[username]:[apikey]#api.softlayer.com/rest/v3/SoftLayer_Virtual_Guest_Block_Device_Template_Group/[imageId]/getObject?objectMask=mask[id,name,children[id,name,blockDevices[diskSpace,units,diskImage[localDiskFlag]]]]
Method:GET
The children items contain the information that you want. Currently, you are trying to get of parentitem.
I hope it help you.