samedi 20 novembre 2021

How to add items from a list of dictionarys to another list using IF conditions

I want to build my own list of dictionaries by getting certain keys and values from another list of dictionaries. I also need to check if some keys exists or not to filter them and if some of them exists, extract only some parts.

Here you are the original list of dictionaries:

messages_raw_json = [
        #item_dict_1
        {
            "_id": "1",
            "msg": "Hi friends",
            "ts": "2021-11-20T06:14:42.374Z",
            "u": {
                "_id": "user1",
                "username": "verz",
                "name": "vrodriguez"
            },
            "_updatedAt": "2021-11-20T06:14:42.586Z",
            "urls": []
        },

         #item_dict_2
        {
            "_id": "2",
            "ts": "2021-11-18T16:52:37.620Z",
            "msg": "",
            "u": {
                "_id": "user2",
                "username": "2cats",
                "name": "Two Cats"
            },
            "_updatedAt": "2021-11-18T16:54:34.285Z",
            "urls": [],
            "t": "msg_removed"
        },

         #item_dict_3
        {
            "_id": "3",
            "ts": "2021-11-18T16:52:37.620Z",
            "msg": "",
            "attachments": [
                {
                    "ts": "1970-01-01T00:00:00.000Z",
                    "title": "image.png",
                    "image_url": "/file-upload/SpacZwkFjWRzdW8eh/image.png",
                    "description": "testing uploading image",
                }
            ],
            "u": {
                "_id": "user3",
                "username": "blas3",
                "name": "blasito"
            },
            "_updatedAt": "2021-11-18T16:54:34.267Z",
            "urls": []
        },

         #item_dict_4
        {
            "_id": "4",
            "t": "user_join",
            "ts": "2021-11-17T20:05:48.043Z",
            "msg": "testing",
            "u": {
                "_id": "user4",
                "username": "micheal11",
                "name": "george"
            },
            "_updatedAt": "2021-11-17T20:05:48.065Z"
        },

         #item_dict_5
        {
            "_id": "5",

            "msg": "Another message here",
            "ts": "2021-11-14T19:59:11.428Z",
            "u": {
                "_id": "user5",
                "username": "Mason78",
                "name": "stuart"
            },
            "_updatedAt": "2021-11-14T19:59:11.770Z",
            "urls": [
                {
                    "url": "https://wa.me/c/12345",
                    "headers": {
                        "contentType": "text/html; charset=\"utf-8\""
                    },
                }
            ]
        }
    ]

My first condition is to check if the item dict includes a key named 't', in that case, I want to skip it. This IF statement handles correctly the situation:

if 't' in message.keys():
   continue

Then I want to get the url from the key 'urls', if this is not empty. And same for 'attachments', I'd like to get only the values from 'title', 'image_url' and 'description' but here is where I'm struggling.

This is the code I wrote which is not working. I understand why (not all the items have the keys I'm trying to retrieve) but honestly, as I'm a beginner in Python, I don't know how to write the other conditions to make it work. Thanks in advance for your help.

messages = []
for message in messages_raw_json:
    if 't' in message.keys():
        continue
    data = {
        'time': message['ts'],
        'name': message['u']['name'],
        'username': message['u']['username'],
        'msg': message['msg'],
        'url': message['urls'][0]['url'],
        'attach_file': message['attachments']['title'],
        'attach_file': message['attachments']['image_url'],
        'attach_file': message['attachments']['description']
        }
    messages.append(data)

print(messages)

Aucun commentaire:

Enregistrer un commentaire