2013-05-02 108 views
1

我有以下的字典,包含以下數據:創建基於另一個字典Python字典

response = {"status":"ERROR","email":"EMAIL_INVALID","name":"NAME_INVALID"} 

我想基於被suposed看起來像「響應」創建一個新的字典以下:

{'api_error': {'list': [{'converted_value': 'No special characters allowed.', 
         'field': 'name', 
         'value': 'NAME_INVALID'}, 
         {'converted_value': 'invalid email', 
         'field': 'email', 
         'value': 'EMAIL_INVALID'}], 
       'status': 'ERROR'}, 
'email': 'EMAIL_INVALID', 
'email_label': 'invalid email', 
'name': 'NAME_INVALID', 
'name_label': 'No special characters allowed.', 
'status': 'ERROR'} 

到目前爲止,我已經能夠做到以下幾點:

ret = {} 
for k in response: 
     if k != 'status': 
       ret[k+"_label"] = convert(response[k]) 
       ret[k] = response[k] 
     else: 
      ret[k] = convert(response[k]) 

其中「CONVER t'函數轉換每個響應值。例如NAME_INVALID被轉換爲'不允許特殊字符'。等等。以下是上述代碼正在執行的輸出:

{"status":"ERROR","name_label":"No special characters allowed.", 
"email_label":"invalid email","name":"NAME_INVALID","email":"EMAIL_INVALID"} 

我遇到了創建字典其餘部分的問題。密鑰爲'api_error'的人。什麼是最有效的方式呢?

+0

哪裏'name_label'價值出臺?我在原始回覆或代碼示例中沒有看到它。 – Bryan 2013-05-02 12:39:27

+0

它來自一個名爲convert的函數,該函數將值轉換爲使用的相應語言。例如對於NAME_INVALID,它返回'不允許特殊字符'。 – hjelpmig 2013-05-02 12:41:47

+3

您可能還想發佈該功能以獲得更完整的答案。 – Bryan 2013-05-02 12:42:39

回答

1
import pprint 

response = {"status": "ERROR", "email": "EMAIL_INVALID", "name": 
      "NAME_INVALID"} 


def convert(label): 
    return {'NAME_INVALID': 'No special characters allowed', 
      'EMAIL_INVALID': 'invalid email', 
      'ERROR': 'ERROR'}[label] 

ret = {} 
for k in response: 
    if k != 'status': 
     ret[k + "_label"] = convert(response[k]) 
     ret[k] = response[k] 
     info = {'converted_value': ret[k + "_label"], 
       'field': k, 
       'value': response[k]} 
     (ret.setdefault('api_error', {}) 
      .setdefault('list', []) 
      .append(info)) 
    else: 
     ret[k] = convert(response[k]) 
     ret.setdefault('api_error', {})['status'] = ret[k] 
pprint.pprint(ret) 

產生

{'api_error': {'list': [{'converted_value': 'invalid email', 
         'field': 'email', 
         'value': 'EMAIL_INVALID'}, 
         {'converted_value': 'No special characters allowed', 
         'field': 'name', 
         'value': 'NAME_INVALID'}], 
       'status': 'ERROR'}, 
'email': 'EMAIL_INVALID', 
'email_label': 'invalid email', 
'name': 'NAME_INVALID', 
'name_label': 'No special characters allowed', 
'status': 'ERROR'} 
+0

謝謝Unutbu!我的問題現在解決了:) – hjelpmig 2013-05-02 13:33:44

0

作一次函數會像

def make2nddict(response): 
for k in response: 
    if k != 'status': 
     d = {} 
     d['converted_value'] = convert(k) 
     d['field'] = k 
     d['value'] = response[k] 
     arr.append(d) 
    else: 
     final[k] = response[k] 
final['list'] = arr 

arr= [] 
final = {} 

def convert(error): 
    if error == 'NAME_INVALID': 
     return 'No special characters allowed' 
    elif error == 'EMAIL_INVALID': 
     return 'EMAIL_INVALID' 
    else: 
     return error 

ret = {} 
for k in response: 
    if k != 'status': 
      ret[k+"_label"] = convert(response[k]) 
      ret[k] = response[k] 
    else: 
     ret[k] = convert(response[k]) 

諾斯功能的認沽輸出在API_ERROR字典 好運

0

這裏全代碼:---

>>>response = {"status":"ERROR","email":"EMAIL_INVALID","name":"NAME_INVALID"} 
>>>def convert(parameter): 
     if parameter == "NAME_INVALID": 
      return "No special characters allowed." 
     if parameter =="EMAIL_INVALID": 
      return "invalid email" 

>>>def newdic(response): 
     ret={} 
     response_keys = response.keys() 
     response_keys.remove("status") 
     if response['status']=="ERROR": 
      ret = response 
      ret['api_error'] ={'list':[],'status':"ERROR"} 
      for key in response_keys: 
       ret[key+"_label"] = convert(response[key]) 
       dic={} 
       dic['converted_value'] = convert(response[key]) 
       dic['field'] = key 
       dic['value'] = response[key] 
       ret['api_error']['list'].append(dic) 

     return ret 

下面是答案: -

>>>newdic(response) 

{'status': 'ERROR', 
'email_label': 'invalid email', 
'name': 'NAME_INVALID', 
'name_label': 'No special characters allowed.', 
'api_error': { 
'email': 'EMAIL_INVALID', 
'list': [{'field': 'email', 'value': 'EMAIL_INVALID', 
      'converted_value': 'invalid email'}, 
      {'field': 'name', 'value': 'NAME_INVALID', 
      'converted_value': 'No special characters allowed.'} 
      ], 
'status': 'ERROR'} 
}