Remove all keys that have values of N/A, -, or empty strings [closed]

2024/11/23 18:11:08
import jsondef clean_data():r = requests.get('https://coderbyte.com/api/challenges/json/json-cleaning')return r.json()print(clean_data())

Example Input

{"name":{"first":"Daniel","middle":"N/A","last":"Smith"},"age":45}

Example Output

{"name":{"first":"Daniel","last":"Smith"},"age":45}
Answer

You can recursively solve this

import json
import requestsr = requests.get('https://coderbyte.com/api/challenges/json/json-cleaning')
data = r.json()def clean_data(data):if(isinstance(data, dict)):to_be_deleted = []for k in data:if(isinstance(data[k], dict)):new_val = clean_data(data[k])data[k] = new_valif(isinstance(data[k], list)):new_val = clean_data(data[k])data[k] = new_valelif(isinstance(data[k], str)):if(data[k] == 'N/A'):to_be_deleted.append(k)for i in to_be_deleted:del data[i]return dataif(isinstance(data, list)):to_be_deleted = []for i in range(len(data)):if(isinstance(data[i], dict)):new_val = clean_data(data[i])data[i] = new_valif(isinstance(data[i], list)):new_val = clean_data(data[k])data[i] = new_valelif(isinstance(data[i], str)):if(data[i] == 'N/A'):to_be_deleted.append(i)for i in to_be_deleted:del data[i]return dataclean_data(data)
data = data = {"name":{"first":"Daniel","middle":"N/A","last":"Smith", "haha": {'hihi':1, "hoho":"N/A"}},"age":45, "hehe":[1,2,3,4,"N/A"]}
clean_data(data)
# {'name': {'first': 'Daniel', 'last': 'Smith', 'haha': {'hihi': 1}}, 'age': 45, 'hehe': [1, 2, 3, 4]}
https://en.xdnf.cn/q/120720.html

Related Q&A

Sorting algorithms more efficient than bubble sort [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

How can I return the odd numbers of a list, using only recursion in Python? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

TypeError: int object is not iterable; Python 2.7

Here is my code:def numbers_in_lists(string):num = int(string)l = list(num)return lstring = 543987When i run it:print numbers_in_lists(string)I have the following error:l = list(num) TypeError: int obj…

Python: Are `hash` values for built-in numeric types, strings standardised?

I came to this question while pondering about the ordering of set, frozenset and dict. Python doesnt guarantee any ordering, and any ordering is coupled to the hash value at some level. But is the hash…

How to create a sample django project?

This doesnt work for me.$ python django-admin.py startproject myprojectI am running a ubuntu virtual m/c on my windows system. By default ubuntu 12.04 comes with python 2.7.3 so I am using that only I …

Why elif statement instead of if statement? [duplicate]

This question already has answers here:Difference between multiple ifs and elifs?(9 answers)Why we use if, else if instead of multiple if block if the body is a return statement(13 answers)Closed 7 ye…

How to understand regular expression with python?

Im new with python. Could anybody help me on how I can create a regular expression given a list of strings like this:test_string = "pero pero CC tan tan RGantigua antiguo AQ0FS0que que CS segn se…

How do I reverse words in a string with Python [duplicate]

This question already has answers here:Reversing words in a Python string (including punctuation)(5 answers)Closed last month.I am trying to reverse words of a string, but having difficulty, any assist…

Reading input files and writing into output files - Python

I have an input file (input.txt) with the following information:Number of students (first line) Number of test scores (second line) list of student names and scoresSo the text file looks something like…

Get strings list in python with regex [duplicate]

This question already has answers here:Split string with multiple-character delimiter(3 answers)Closed 6 years ago.I want extract strings from this text with regex:~ZCC0ZAF~World~AAEef~RZgthAD~AAjaKNed…