0

Here is the above dictionary I need to remove fields like 'timestamp' and field name 'updated_date' inside attributes

How can i do it?

I'm using lot of loops thought that was not a good solution was checking for few code snippets on same to achieve this ?

I'm using python version 2.7 dict:

[{'name': 'mark', 'class': '9thgrade', 'timestamp': '2023-06-05 18:27:23.326', 'reg_no': 651,
          'attributes': '{"updated_date": "2023-06-05 18:27:22.543060", "hobbies": "" : " ", "sports": "cricket" : " ",'
                        ' "subjects":  {"": u"social", u"chemisty", u"cats": " "}, '
                        '"address":  {"cats": "UK,london"}}'},
         {'name': 'mark', 'class': '9thgrade', 'timestamp': '2023-06-05 18:35:32.386', 'reg_no': 661,
          'attributes': '{"updated_date": "2023-06-05 18:35:31.609323", "hobbies": "" : " ", "sports": "cricket" : " ", '
                        '"subjects":  {"science,maths"},'
                        ' "address":  {"texas,us"}'}]

I did try like below code but the return type is string :

for items2 in db_read:
    # result_new=items.pop('attributes')
    result_new = items2.get('attributes')
    print(type(result_new))
    print(result_new)
kumar Raj
  • 13
  • 8
  • Does this answer your question? [How can I remove a key from a Python dictionary?](https://stackoverflow.com/questions/11277432/how-can-i-remove-a-key-from-a-python-dictionary) – Rafael-WO Jun 06 '23 at 07:36
  • You _could_ try to use `eval()`, but your nested `attributes` dictionaries are malformed. Why? – B Remmelzwaal Jun 06 '23 at 07:42
  • How is `"hobbies": "" : " "` supposed to be interpreted? – treuss Jun 06 '23 at 07:50
  • The attributes value is a string. The string is not a representation of either JSON or a Python dictionary. The only way to remove 'updated_date' is with a string replacement / removal – DarkKnight Jun 06 '23 at 07:52
  • Python 2,7 was replaced years ago and numerous changes were brought in; why are you still using it? Latest version is 3.11.3. – user19077881 Jun 06 '23 at 07:59

0 Answers0