P
P
Paul2019-11-17 17:41:36
Python
Paul, 2019-11-17 17:41:36

How to select desired elements in nested python dictionaries?

Hello everyone, friends,
I'm experimenting with the contact API) I'm
downloading posts from a group for data analysis. For example, I received a dictionary with the following posts:
Here I uploaded json to pastebin https://pastebin.com/HnReC414
I receive and add the following data to the new dictionary:

posts = otvet_json["response"]['items']         #   Сохраняем в переменную все посты
all_posts = []  # Подготовили пустой список для сохранения туда всех данных
filtered_data = []     # Подготовили пустой список для сохранения туда всех отфильтрованных данных
for posts in all_posts:
        id = posts["id"]
        date = time.ctime(posts["date"])
        linuxe_date = posts["date"]
        text = posts["text"]
        likes = posts["likes"]["count"]
        comments = posts["comments"]["count"]
        reposts = posts["reposts"]["count"]
        views = posts["views"]["count"]
        marked_as_ads = posts["marked_as_ads"]
       


        filtered_posts = {
                "id": id,
                "date" : date,          #   Создаем ключ date и записываем туда значение из переменной date
                "linuxe_date" : linuxe_date,            #   Создаем ключ linuxe_date и записываем туда значение из переменной linuxe_date
                "text" : text,          #   Создаем ключ text и записываем туда значение из переменной text
                "likes" : likes,        #   Создаем ключ likes и записываем туда значение из переменной likes
                "comments" : comments,          #   Создаем ключ comments и записываем туда значение из переменной comments
                "reposts" : reposts,          #   Создаем ключ reposts и записываем туда значение из переменной reposts
                "views" : views,          #   Создаем ключ views и записываем туда значение из переменной views
                "marked_as_ads" : marked_as_ads,          #   Создаем ключ marked_as_ads и записываем туда значение из переменной marked_as_ads


        }
        filtered_data.append(filtered_posts)

How to isolate all photos from all posts of the maximum size, i.e. "type": "x" and then add all this to filtered_data ?
I think the algorithm should be something like this:
try:
                attachments = posts["attachments"]
                for att1 in attachments:
                        if att1["type"] == "photo":
                                for razmerphoto in att1:
                                        if razmerphoto["photo"]["sizes"]["type"] == "x":
                                Дальше не знаю как правильно прописать.....
        except:
                attachments = "netu"

Also, while it is not clear to me how to weed out advertising posts? Some posts have "marked_as_ads" attribute: 1 I.e. you need to somehow make an algorithm so that if "marked_as_ads": 1, then this entire post is immediately eliminated.
I think the algorithm should be something like this:
for posts in all_posts:
        if pppposts["marked_as_ads"] == 1:
                del posts

Many thanks in advance, friends.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2019-11-17
@o5a

How to isolate all photos from all posts of the maximum size, i.e. "type": "x" and then add all this to filtered_data ?

Take the last size. More or less like this
It is better not to delete, but simply filter your cycle (skip these posts in your cycle).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question