Skip to content

Instantly share code, notes, and snippets.

@marceloandriolli
Last active July 21, 2017 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marceloandriolli/06674bc9e1fbd244a465ba0f1dd05fd5 to your computer and use it in GitHub Desktop.
Save marceloandriolli/06674bc9e1fbd244a465ba0f1dd05fd5 to your computer and use it in GitHub Desktop.
raw = {u'pf': {u'dados': {u'protocolo': u'fd6be408-a519-41e6-8beb-55712e2b79a6', u'ccf': u'NAO', u'data_nasc': u'1985-06-01', u'situacao_receita_federal': {u'situacao': u'REGULAR', u'dataconsulta': u''}, u'nome': u'MARCELO RODRIGO DOS SANTOS ANDRIOLLI', u'enderecos': {u'endereco': [{u'tipo_logradouro': u'AV', u'complemento': u'AP 302', u'bairro': u'KOBRASOL', u'cidade': u'SAO JOSE', u'logradouro': u'LEDIO JOAO MARTINS', u'numero': 584, u'score': 8, u'cep': 88101100, u'uf': u'SC'}, {u'tipo_logradouro': u'SRV', u'bairro': u'CAMPINAS', u'cidade': u'SAO JOSE', u'logradouro': u'ELVIRA SCHMIDT GOEDERT', u'numero': 8, u'score': 8, u'cep': 88101335, u'uf': u'SC'}]}, u'signo': u'G\xcaMEOS', u'cpf': 4353350900, u'faixa_renda_estimada': u'D', u'idade': 32, u'probabilidade_obito': 0, u'telefones_moveis': {
u'telefone': [4832411640, u'', u'', 48984833639, 48996219722, u'']}, u'mae': {u'nome': u'JANETE FRANCISCA DOS SANTOS'}, u'ocupacoes': {u'ocupacao': {u'telefones': {u'telefone': [8134650705, 4821080953, 4821080954, 4821080955]}, u'cnpj': 82901000000127, u'faixa_salario': u'D', u'descricao_cnae': u'Fabrica\xe7\xe3o de aparelhos telef\xf4nicos e de outros equipamentos de comunica\xe7\xe3o, pe\xe7as e acess\xf3rios', u'razao_social': u'INTELBRAS S.A. INDUSTRIA DE TELECOMUNICACAO ELETRONICA BRASILEIRA', u'salario': u'Entre 2 e 4 SM', u'porte': u'GRANDE EMPRESA', u'codigo': 214365, u'cnae': 2632900, u'descricao': u'Tecn\xf3logo em eletr\xf4nica'}}, u'renda_estimada': 2688, u'sexo': u'M', u'emails': {u'email1': u'marcelorsa@gmail.com'}}}}
def key_dimension(key_list, dictionary):
for key in key_list:
if dictionary.get(key):
dictionary = dictionary.get(key).copy()
else:
return None
return dictionary
nested_keys = ['pf', 'dados', 'telefones_moveis', 'telefone']
r = key_dimension(keys, raw)
print(r)
nested_keys = ['pf', 'dados', 'ocupacoes', 'ocupacao', 'telefones', 'telefone']
r = key_dimension(keys, raw)
print(r)
@marceloandriolli
Copy link
Author

Works on Python 3.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment