I'm having some trouble extracting info from a Python object. Basically, using notation like this works to get down to values within a dict I am working with:
clean_content['Al38zGKg6YC4']['image']
I was expecting to see another nested dict which contained the key/value that I wanted to extract. However, what's there is a list that looks like a dict[1]
I'm looking to extract the 'permalink' field from this list, and then tie it back to the page ID in the original dict. Any suggestions?
I came to a working solution as follows: converting the original dictionary into a pandas dataframe, I extracted the column containing the lists of image data. Each of these lists contained URLs for multiple images, so I extracted the one required using dicts:
image_df_temp = {}
image_df_url = {}
for i in range(len(image_df_base.index)):
image_df_temp[i] = pd.DataFrame(image_df_base.image[i])
image_df_url[i] = image_df_temp[i].iloc[0]
Finally, I edited the dicts missing the required metadata using .get:
for i in range(len(image_df_base.index)):
image_df_url[i] = image_df_url[i].get('permalink', "No image available")
Related
we all know that if we need to retrieve data from the database the data will back as a queryset but the question is How can I retrieve the data from database which is the name of it is queryset but remove that name from it.
maybe I can't be clarified enough in explanation so you can look at the next example to understand what I mean:
AnyObjects.objects.all().values()
this line will back the data like so:
<QuerySet [{'key': 'value'}]
now you can see the first name that is on the left side of retrieving data which is: "QuerySet" so, I need to remove that name to make the data as follows:
[{'key': 'value'}]
if you wonder about why so, the abbreviation of answer is I want to use Dataframe by pandas so, to put the data in Dataframe method I should use that layout.
any help please!!
You don't have to change it from a Queryset to anything else; pandas.DataFrame can take any Iterable as data. So
df = pandas.DataFrame(djangoapp.models.Model.objects.all().values())
Gives you the DataFrame you expect. (though you may want to double check df.dtypes. If there are Nones in your data, the column may end up to be of object type.)
You can use list(…) to convert it to a list of dictionaries:
list(AnyObjects.objects.values())
You will need to serialize it with the json package to obtain a JSON blob, since strings with single quotes are not valid JSON, in order to make it a JSON blob, you can work with:
import json
json.dumps(list(AnyObjects.object.values()))
I have a Project and when searching a query I need to split the data (not search query) in to words and apply searching.
for example:
my query is : 'bot' (typing 'bottle')
but if I use meta_keywords__icontains = query the filter will also return queries with 'robot'.
Here meta_keywords are keywords that can be used for searching.
I won't be able to access data if the data in meta_keywords is 'water bottle' when I use meta_keywords__istartswith is there any way I can use in this case.
what I just need is search in every words of data with just istartswith
I can simply create a model for 'meta_keywords' and use the current data to assign values by splitting and saving as different data. I know it might be the best way. I need some other ways to achieve it.
You can search the name field with each word that istartswith in variable query.
import re
instances = Model.objects.filter(Q(name__iregex=r'[[:<:]]' + re.escape(query)))
Eg: Hello world can be searched using the query 'hello' and 'world'. It don't check the icontains
note: It works only in Python3
What I have:
A CSV which I populated with rows of text, which are one word per cell.
Micro level: Attempting:
I am trying to create a dictionary where each row is a Key and each Key is assigned an empty list as a variable (see below).
I can do this one row at a time by converting the list to a tuple -->
creating an empty list -->
Adding the tuple to my dictionary as a key and assigning the empty list as the variable
However, I would like to do this in an automatic fashion as doing this individually is tedious.
Macro level: Attempting:
I want to assign a list of keywords (tags) to each row in my CSV to call upon the text later based on their tags.
My question:
Is there a way to do this the way I am describing?
Am I going about it wrong and should be doing this a different way?
*edit: I am thinking that if I flip this I could solve my overall issue.
For example make x amount of tags as key values for my tag dictionary and make a one time run through to assign each key with a empty dictionary value. Then populate the dictionaries with the text from my CSV.
This would not remove the one by one method; however, would reduce the amount of times I would need to enter Key/Value pairs as I am more likely to have more text than tags.
see code below
!#Python3
import csv
import os
import string
#open CSV and assign var to the list content
outputFile = open("output.csv", encoding="utf-8")
outputReader = csv.reader(outputFile)
data = list(outputReader)
#Get rid of empty cells
for list in data:
for object in list:
while "" in list:
list.remove("")
#open a dictionary
tags = {}
#Turn first row of CSV into a tuple
article1 = tuple(data[1])
#generate empty list
article1_tags = []
#Assign empty list as a variable to the article1 Key and put in tags dictionary
While True:
if article1 in tags :
break
else:
tags[article1] = article1_tags
Now that I have bit more of idea of what you are trying to acheive I would suggest using a list of dictionary's. Each dictionary containing the data about each article (or row from your csv file). The key here is that a csv file is still a plain text file, there is nothing special about csv. In fact I would avoid using excel altogether and edit using a text editor.
I would start by opening the file and reading each row (line) from the file into a key/value pair of a dictionary.
The cool thing about python 3 is that you do that very easily without extra modules.
csvfile = open('output.csv', encoding='utf-8')
articlelist = []
for line in csvfile:
articlelist.append(dict(textkey=line,tagskey=[]))
Using the iterator 'line' in this context with a text file stream object will automatically go row by row and take all the text of that line as a single string. So line is a string object here.
Once you have list of dictionary's like this you can simply iterate through the articlelist printing out or adding tags or doing whatever you wish even adding more key/value pairs to each dictionary. Doing it in this way means that not all the dictionary's need to follow the same format (although thats desirable).
I added the tagskey key and the value is an empty list which you can add to later.
Do not use infinite while loops or while loops at all to go through lists etc. Always use the
for iterator in theList:
method.
I would also look into using the JSON format for your little exercise here. I think it will lend itself much nicer to what you are trying to acheive. And with Python JSON is very easily read and then output again all using plain text. You could then output to a JSON text file manually edit it and then python read it again and process it.
I hope this helps.
I want to get the first value from a sequence for a key within a dictionary object.
This is the dictionary object:
{0: (mdb.models['EXP-100'].parts['hatpart-100'].vertices[1],
(62.5242172081597, 101.192447407436, 325.0))}
The Key is 0, and I'd like to get the first value in the sequence of values
(vertex object and coordinates), specifically the vertex object.
When you're trying to get a specific item from a dictionary containing a list you can do it by indicating the key and then index that you're looking for. So:
your dictionary ={'key':[1,2,3]}
result = your_dictionary['key'][0]
where key is the first item you want and if it contains a list, then the "0" is asking for the first item of the list.
The above will return the value "1".
You can iterate over this to get all values and you can add as many [] as you need to get to the specific value you need.
That being said, your data doesn't look quite right. The dictionary should contain "key":"object" sets. Those objects can be further dictionaries themselves or lists, but what you've provided seems to be off. You may have to manipulate the data to fit what you need it to do before your can perform any operations on it. This could help with that first: "Deparsing" a list using pyparsing
I'm parsing a dictionary in Django from the request.
I know I can extract the value using the key name like so: myDict=request_data['myDict']
now i would like to do this obj1= myDict[0] instead of obj1= myDict['obj1']
but it doesn't seems to work, ant suggestions?
Different from a list a dictionary has 'keys'. I'm guessing you are referring to the iterating over the different keys that your dictionary has.
what you want to do is something like this:
keys = myDict.keys()
obj1 = myDict[keys[0]]